gen
gen

Reputation: 10040

Correct syntax of let ... in and where clauses in Haskell

I am trying to declare local variables (is this the right term in the case of haskell?) in haskell using where and let-in clauses. However whenever my clauses are longer than a single line I always get some parse errors:

> letExample :: Int -> Int
> letExample 0 = 0
> letExample n =
>               let one     = 1
>                   four    = 4
>                   eight   = 8
>               in one*four*eight

When trying to load the above code into ghci I get the following error:

letexample.lhs:4:33:
parse error in let binding: missing required 'in' Failed, modules loaded: none.

I get the following error when trying to load the code below:

whereexample:5:57: parse error on input ‘=’
Failed, modules loaded: none.

code:

> whereExample :: Int -> Int
> whereExample 0 = 0
> whereExample n = one * four * eight
>               where   one     = 1
>                       four    = 4
>                       eight   = 8

What is the right way of using let and where in the above cases?

Upvotes: 0

Views: 273

Answers (1)

chi
chi

Reputation: 116139

The posted code mixes tabs and spaces. This is a frequent cause of indentation problems.

The Haskell report, which defined the language, states that tabs are equivalent to 8 spaces. If your editor is configured to display tabs as another number of spaces, chances are that what you are reading as indented at the correct level is not actually so for the compiler.

The easiest fix is to replace tab with spaces in the source files. For that, I recommend turning on warnings passing the -Wall flag, or by adding

{-# OPTIONS -Wall #-}

at the beginning of you source files. Doing that will cause GHC to warn when tabs are detected.

There also are alternative solutions to the drastic remove-all-tabs one. There are smart ways to mix tabs and spaces in a "tab-agnostic" way, which will make the code compilable and readable in spite of how many spaces a tab is equivalent to. While is is not very popular, such proposals have their technical merits.

Upvotes: 2

Related Questions