0

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?

gen
  • 9,528
  • 14
  • 35
  • 64
  • 14
    Your posted code mixes tabs and spaces, GHC treats tabs as 8 spaces, and this breaks indentation. I recommend turning on warnings `-Wall` so that this is reported by the compiler, so you know there's something wrong. A simple solution is to avoid tabs. For more solutions see http://dmwit.com/tabs/ – chi Feb 08 '16 at 10:58
  • 1
    **Never** mix tabs and spaces. It breaks code in all languages where indentation is significant. Check your editor settings and either always use only tabs or only spaces (the latter is usually preferred). – Bakuriu Feb 08 '16 at 12:28
  • And for the record, these examples as displayed on StackOverflow appear to have exactly correct layout and syntax. – Reid Barton Feb 08 '16 at 14:59
  • And correct term is bindings, not variables. – lehins Feb 09 '16 at 08:19
  • thanks @AlexeyKuleshevich – gen Feb 09 '16 at 08:35
  • @chi can you post your comment as an answer? It resolved my issue... – gen Feb 11 '16 at 16:56
  • "Variables" is okay too. – dfeuer Feb 11 '16 at 19:53

1 Answers1

2

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.

chi
  • 111,837
  • 3
  • 133
  • 218