Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Simple Haskell question

Name: Anonymous 2011-07-12 15:24

Hi /prog/,

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: Anonymous 2011-07-13 0:31

Nomads? In my /prog/?

polecat kebabs

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List