Reputation: 1631
I have just started using Haskell interpreter.
Can anyone tell me what's wrong with this code:(for Fibonacci)
fib :: Int -> Int
fib n
|n==0 =0
|n==1 =1
|n>1 =fib(n-2) + fib(n-1)
I am getting this error message:
fib.hs:3:1: parse error on input `|'
[1 of 1] Compiling Main ( fib.hs, interpreted )
Failed, modules loaded: none.
Upvotes: 0
Views: 459
Reputation: 116139
You have to indent the next lines at least one space, e.g.:
fib :: Int -> Int
fib n
|n==0 =0
|n==1 =1
|n>1 =fib(n-2) + fib(n-1)
Upvotes: 1
Reputation: 18189
Haskell is indentation sensitive. In particular the |
's need to be indented further than the function name:
fib :: Int -> Int
fib n
|n==0 =0
|n==1 =1
|n>1 =fib(n-2) + fib(n-1)
Also, it is more idiomatic to use pattern matching for comparing with a constant:
fib :: Int -> Int
fib 0 =0
fib 1 =1
fib n
|n>1 =fib(n-2) + fib(n-1)
Upvotes: 4