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
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
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.
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









