开发者

Haskell Error - Naked Expression at Top Level

开发者 https://www.devze.com 2023-03-23 20:24 出处:网络
I have the following code: fib n n == 0= 0 n == 1= 1 n > 1= fib (n-1) + fib (n-2) print fib 5 And for some reason, it\'s throwing an error:

I have the following code:

fib n
    | n == 0  = 0
    | n == 1  = 1
    | n > 1  = fib (n-1) + fib (n-2)

print fib 5

And for some reason, it's throwing an error:

[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:8:1: Parse error: naked expression 开发者_JAVA技巧at top level

What's going on?


You cannot have an expression at the top-level. Haskell program entry point is a main function in Main module. Also print fib 5 calls print with two arguments, you need to do:

main = print $ fib 5

or

main = print (fib 5)
0

精彩评论

暂无评论...
验证码 换一张
取 消