|
|
|
|
|
by paulgb
6519 days ago
|
|
Agreed, map is a good example only if the person reading it gets first-class functions. I can only think of a couple other suggestions for simple recursive functions. Euclid's algorithm for GCD would be a good one. Fibonacci numbers are easily expressed with recursion, but the most obvious solution is unfortunately not tail recursive and inefficient. My favorite example of mutual recursion is "even" and "odd": even 0 = True
even x = odd (x - 1) odd 0 = False
odd x = even (x - 1) Inefficient, of course, but easy to prove correctness even to someone who's brain hasn't yet clicked into understanding recursion. Length of a list is as boring as factorials, but I guess that would be one too. Same with generating a list of counting numbers. |
|
Euclid's algorithm is a really good example, by the way. Thanks.