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?

Marked As Solved

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)

Also Liked

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.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement