why bellow function doesn't produce an array of natural numbers from n to 0?
Prelude> let foo n = if n > 0 then n : (foo n-1) else []
Instead I got rather cryptic message:
Prelude> foo 5
<interactive>:1:0:
No instance for (Num [t])
arising from a use of `foo' at <interactive>:1:0-4
Possible fix: add an instance declaration for (Num [t])
In the expression: foo 5
In the definition of `it': it = foo 5
How to fix this? Why this failsand bellow code works?
Prelude> 1 : 2 : []
[1,2]
Name:
!!kCq+A64Losi56ze2011-07-12 16:21
Okay, I'm not the Haskell master here, but don't you need to define a base case for n?