I am trying to implement collatz-list using Haskel:
Here’s my code:
collatz n
| mod n 2 == 0 = div n 2
| otherwise = 3 * n + 1
collatzList n
| n < 1 = error "Cannot have negative number"
collatzList
| n == 1 = [n]
| otherwise = n:collatzList (collatz n)
The error message I am getting is this:
parse error on input `collatzList’
[1 of 1] Compiling Main ( exer.hs, interpreted )
Failed, modules loaded: none.
Can anyone tell me why I am getting this message?
I get different errors (using GHC 7.4.1):
This is because you forgot the
nargument in your second equation forcollatzList. You can either add this argumentor, since the left hand sides are now the same, you can simply join it with the first one: