I'm trying to declare an immutable variable in Haskell:
let a = [1, 2]
main = print $ sum a
but it claims
parse error (possibly incorrect indentation)
what's up with that?
let is not used at the top level definition. There are several ways to correct your programs, some of which are
a = [1,2]
main = print $ sum a
Or
main = do
let a = [1,2]
print $ sum a
Or
main = let a = [1,2] in print $ sum a
The usual source of confusion for people trying to use let at the top level is when they are trying to convert some tested expression in ghci to actual source file.
let can be used when you are working inside a monad. ghci and main works inside IO monad, so you can write something like let a = [1,2] in ghci.
let is also used for let ... in ... expressions, e.g. main = let a = [1,2] in print $ sum a would be another valid alternative.let construct, this answer is more useful to anyone who happens to see this question when trying to find information about that syntax.