Haskell Error - Naked Expression at Top Level

人走茶凉 提交于 2019-12-03 14:31:28

问题


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 at top level

What's going on?


回答1:


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)


来源:https://stackoverflow.com/questions/6885148/haskell-error-naked-expression-at-top-level

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!