Aries_is_there
Aries_is_there

Reputation: 386

Haskell: parse error on input ‘=’ in where

I am new in Haskell and practicing Algorithm in Haskell follow the book "Pearls of Functional Algorithm Design"

This is the algo to find the smallest natural number not in a given finite set X of natural numbers

import Data.List
import Data.Array

minfree xs = if null ([0..b-1] \\ us)
             then head ([b..] \\ vs)
             else head ([0..b-1] \\ us)
             where (us, vs) = (partition (<b) xs)
                          b = div (length xs) 2

Pff the compiler error like this

Prelude Data.Array Data.List> :load 01_the_smallest_free_number.hs 
[1 of 1] Compiling Main             ( 01_the_smallest_free_number.hs, interpreted )

01_the_smallest_free_number.hs:11:29: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'
   |
11 |                           b = div (length xs) 2 
   |                             ^
Failed, no modules loaded.

Well obveriously add let before b is not the right answer I've tried.

Then I replaced all b to div (length xs) 2 that works, so seems here is the problem to be, but I don't get it

Upvotes: 3

Views: 332

Answers (1)

chi
chi

Reputation: 116139

This is bad indentation:

     where (us, vs) = (partition (<b) xs)
                  b = div (length xs) 2

Since the second equality is indented more than the first, it continues the first one, as if we wrote

     where (us, vs) = (partition (<b) xs) b = div (length xs) 2

triggering the error.

You instead want:

     where (us, vs) = (partition (<b) xs)
           b        = div (length xs) 2

so that both equations are indented in the same way.

Upvotes: 7

Related Questions