bendevski
Recursion using only original list
I’m playing around with elixir and solving some leetcode problems but I ran into an issue. The following code:
defmodule Solution do
@spec climb_stairs(n :: integer) :: integer
def climb_stairs(n) do
climb_stairs(n, [1,0])
end
def climb_stairs(1, lis) do
[first, second | _] = lis
first + second
end
def climb_stairs(n, lis) do
[first, second ] = lis
climb_stairs(n-1, [first + second, second])
end
end
always has lis be [1,0] (the result is always 1). Could someone explain what I’m doing wrong?
Marked As Solved
LostKobrakai
You never actually change the list:
lis = [1, 0] # this is what you start with
# hitting the first clause of `climb_stairs/2` you do:
[1, 0 | _] = lis
1 + 0
# returns 1
# hitting the second clause of `climb_stairs/2` you do:
[1, 0] = lis
climb_stairs(…, [1 + 0, 0])
# calls `climb_stairs` again with `[1, 0]`
# eventually this reaches the first clause getting to the above case.
1
Also Liked
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Hi @bendevski welcome! It would be helpful if you could explain what this function is supposed to do.
1
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I would like to know what is the best IDE for elixir development?
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Other popular topics
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







