DidactMacros

DidactMacros

Programming Elixir book, Lists and recursion question

I’ve been working my way through the programming elixir book, and I wanted to ascertain whether or not the way I answer a particular question was in line with what was intended.

Some of the question sections seem to pertain more so to learning sections two steps prior, rather than the immediately preceding section.

For example I just finished the question:

Write a function MyList.span(from, to) that returns a list of the numbers from
from up to to.

The below…

  def span(from, to) when not is_integer(from) or not is_integer(to) do
    IO.puts("two integers required");
  end

  def span(from, to) when from == to, do: [to]

  def span(from, to) when from < to do
    [from|span(from + 1, to)]
  end

  def span(from, to) when from > to do
    [from|span(from - 1, to)]
  end

…does the job, but the actual reading section itself was regarding list recursion as it pertained to filtering, and using recursive pattern matching, that is [head = [_, hello, _, _] | func(tail)]. I did not have to do either of these things here, not even to make to make the code look cleaner.

Did I not understand the question? Could I have tackled the problem so as to be more inline with what I had just learnt?

First 4 of 4 Posts! Switch mode

ken-kost

ken-kost

But you did use recursive pattern matching.? i.e. you call span in span and pattern match your way out when from == to (you could’ve also matched it like span(to, to) indicating both are same value by matching it to one ‘to’ variable)

DidactMacros

DidactMacros

I understand, but this was already covered in prior chapters, and questions were already given that required such implementation (and more).

Btw, thanks for the tip regarding span(to, to) I’ll definitely be using that in other implementations.

Would my implementation benefit from nested/chained pattern matching, that is, something like [head = [_, hello, _, _] | func(tail)]?

al2o3cr

al2o3cr

I can’t speak to the specific intent in this case, but a common tactic in teaching is to repeat material (or exercises) after a pause to improve retention.

DidactMacros

DidactMacros

Thanks everyone, I did suspect this to be the case, I just wanted to make sure I wasn’t missing the point with this question or others as it had happened with a couple of prior chapters.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement