|
|
|
|
|
by silentbicycle
6515 days ago
|
|
Agreed, but in a parallel universe where the parent post actually asked the implied question, what other functions would you suggest? I try to explain these things to other people enough that I'd love to hear other suggestions. Map might be another good one, although for better and worse it expects the reader to follow the idea of first class functions. Something like map square [1, 2, 3, 4, 5] ---> [1, 4, 9, 16, 25]
is pretty simple, though. |
|
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.