Haskell/Solutions/Recursion

The factorial function

 * 1)  can be defined as follows:
 * 2) Typing   should yield the message  .  This is because, according to the definition,   is , which is  . This will never stop, so the function will keep going until it runs out of memory.
 * 3)   can be defined as follows:

Other recursive functions
1. 5 × 4:
 * 4 isn't 1, so we recurse: work out 5 × 3
 * 3 isn't 1, so we recurse
 * 2 isn't 1, so we recurse
 * 1 is 1, so we return 5
 * We add the current number, 5, to the result of the recursion, 5. We get 10
 * We add the current number, 5, to the result of the recursion, 10. We get 15
 * We add the current number, 5, to the result of the recursion, 15. We get 20.

2.

3.

4.

List-based recursion
The answers for 1-3, in one block of code:

does not match on an empty list so you can also have:

And here is the accumulating :