I have the following code that works well on command line:
ghci> [let square x = x * x in (square 5, square 3, square 2)]
[(25,9,4)]
but when I have it in a file and compile it, it doesn't work and I get an error:
[1 of 1] Compiling Main ( baby.hs, interpreted )
baby.hs:62:1:
Parse error: naked expression at top level
Perhaps you intended to use TemplateHaskell
Failed, modules loaded: none.
The REPL, GHCi, accepts Haskell expressions. Unlike Python, a Haskell module must be composed of declarations.
For example, an expression could be 1+1
but even from a human perspective that makes no sense as a top-level of a source file - there is no variable declared and no operation performed. At the top level you can declare a value, such as:
val = [let square x = x * x in (square 5, square 3, square 2)]
Or in the REPL you can do the same but with let
:
let val = [let square x = x * x in (square 5, square 3, square 2)]