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
Last Post!
bendevski
Thank you for the clear explanation. Mixed up the variables in my head and thought it was a language thing.
0
Popular in Questions
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
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
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
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
Other popular topics
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I would like to know what is the best IDE for elixir development?
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









