3 ms·
Pattern matching is the process of defining multiple functions with the same number of arguments, where the arguments decide which function to execute. E.g.: f
by adamtulinius 15y ago
Pattern matching is the process of defining multiple functions with the same number of arguments, where the arguments decide which function to execute.
E.g.:
foo(0) -> 1;
foo(n) -> foo(n) * foo(n-1).
calls to foo(0) would execute the first function, while all other calls would execute the 2nd. Note that it is often necessary to define the functions in order of most specific first.
A similar concept in Erlang is guards, where an expression can be evaluated instead of simple pattern matching.
foo(n) where n == 0 -> 1;
foo(n) -> foo(n) * foo(n-1).
Might have screwed up on the Erlang notation in that example though.
- adamtulinius 15y agoThis is why you don't write code before your morning shot of caffeine (unending recursive function calls). Meh.