I can't recall writing a recursive method in Ruby. I think that's because I find recursion more confusing than iteration (ie
Array#map). Additionally, recursion isn't efficient in an imperative language like Ruby.
While I find recursive functions a bit difficult to grok in any language, writing a recursive function in Elixir is a good exercise for 3 reasons:
- Unlike Ruby, there isn't a performance hit when you use recursion in Elixir.
- Exposure to the inner workings of the
- Exposure to pattern matching in multiple ways.
I'll start with some background on
List and pattern matching, then write a function to sum the contents of a
List. Finally, we'll write a recursive map function.