So I've been playing with Haskell the past couple of days, and I decided I'd make a basic definition of the Fibonacci sequence. So I wrote this code:
main = do
fib :: (Integral a) => Int -> Int
fib x
| x == 0 = 0
| x == 1 = 1
| x >= 2 = fib (x - 2) + fib (x - 1)
do { print (fib 5) }
And I get an error message saying:
4:17: parse error on input `|'
I suspected tab errors, so I tried every whitespace fix I could find, but I just can't find what's wrong!
EDIT: So I did what people suggested, and I have this code now:
fib :: (Integral a) => Int -> Int
main = do
fib x
| x == 0 = 0
| x == 1 = 1
| x >= 2 = fib (x - 2) + fib (x - 1)
print (fib 5)
And I'm getting the same error.
You can also define
fib
locally tomain
outside of thedo
block. Do bear in mind thatdo
is syntactic sugar for the use of various monadic binding functions, and so the syntax accepted within it is not quite the same as that accepted outside it. And, in fact, yourmain
doesn't even require thedo
block because you just callprint
rather than chaining anyIO
actions together.Or you could use
where
:They're the same, the question is just where the local binding actually goes.
let
..in
gives you a new block where the new bindings are in scope, whilewhere
makes its bindings available in the scope of the function it's attached to.If, as seems eventually likely, you do want a
do
block as well so you can do multipleIO
actions, you can just put that in place of the call toprint
, like so: