bendevski

bendevski

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?

Showing Posts 1 to 3

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.

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.
bendevski

bendevski OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews