bendevski

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

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.

Also Liked

benwilson512

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.

Last Post!

bendevski

bendevski

Thank you for the clear explanation. Mixed up the variables in my head and thought it was a language thing.

Where Next?

Popular in Questions Top

vertexbuffer
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
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
9mm
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
pmjoe
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
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 Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
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
saif
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
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

We're in Beta

About us Mission Statement