

Using Recursion in Elixir - groundCode
http://rob.conery.io/2015/09/04/using-recursion-in-elixir-to-break-your-oo-brain/

======
zzalpha
The author seems confused, as the code here doesn't actually involve
recursion. I wouldn't normally nitpick too hard, here, but he spends a lot of
time talking about how Elixer made it possible for him to understand and use
recursion... The trouble is neither of these things seemed to have happened...

~~~
davidstump
I believe he is referencing the fact that the map_single function calls
itself.

~~~
zzalpha
Gah, indeed it does! I pored over that code to find it and it was right
there...

The post still seems to be more about pattern matching than recursion, but at
least recursion is in there somewhere!

~~~
segmondy
Actually, you are correct. There is no recursion. It's perform pattern
matching, the functions might have the same name, but it's not recursive,
because they are different functions.

Let's pretend we can alias these functions using AS

    
    
      def map_single({:ok, res}) AS fun1
    
      def map_single({:cols_and_first, cols, first_row}) AS fun2
    
      def map_single({:zipped, list}) AS fun3
    
      def map_single({:error, err}) AS fun4
    
    

Notice how it's called.

fun1 calls fun2

fun2 calls fun3

fun4

to be recursive, a function must call itself or another function that calls
it. neither of this is true. What the article is talking about is pattern
matching.

~~~
zzalpha
Yup you're right. I thought they were mutually recursive but they're not.

------
tfigment
I hate to be one of those that complain about the website itself and not the
content but this one annoyed me greatly.

This site is another one that requires javascript to see any content for no
apparent reason. Worse when scrolling on a narrow window (so that search is
not on the right) like on a tablet, when you scroll up it jumps to the top
rather than scrolling up so you lose where you are. When you scroll back down
it jumps back to where it was so its hard to scroll up reliably.

Also regarding the content, I actually prefer the original code for some
reason over the multitude of helper functions. But I guess that is a matter of
style.

------
segmondy
I'm sorry but that article is terrible. I don't even program in elixir, but if
you are trying to don't even use this. There is no recursion, just pattern
matching.

~~~
speedkills
Serious question, is pattern matching in Elixir any different from
overloading?

~~~
ericmj
Pattern matching and function overloading based on types are different but can
be used for the same purposes.

For function overloading the decision for which implementation to use usually
happens at compile time along with the type checking. Pattern matching happens
at runtime but is more powerful since you can pattern match based on you
guessed it: patterns, instead of just types.

Another small difference is that with overloading multiple functions with
different signatures are created (they just share name) but with pattern
matching there is a single function with multiple "function clauses".

~~~
speedkills
Great explanation. Thank you.

