row248
row248

Reputation: 119

Parse error in where

In this code

neighbours :: CityMap -> District -> [District]
neighbours (CM (_,rs)) b = mapMaybe neighbour rs
    where neighbour (p,q)
        | b == p    = Just q --parse error (possibly incorrect indentation or mismatched brackets)
        | b == q    = Just p
        | otherwise = Nothing

i have parse in first «|»

Upvotes: 1

Views: 173

Answers (1)

AndrewC
AndrewC

Reputation: 32475

The guards have to be indented further than the name of the function they're part of, for example:

neighbours :: CityMap -> District -> [District]
neighbours (CM (_,rs)) b = mapMaybe neighbour rs
    where neighbour (p,q)
           | b == p    = Just q 
           | b == q    = Just p
           | otherwise = Nothing

This is because following the where, you're defining a (local) function neighbour, which has to also follow the layout rule; if the guard is further to the left, it's not a continuation of the definition of neighbour. You'd get the same error in a file that looked like this:

  neighbour (p,q)
| b == p   = Just q

Upvotes: 4

Related Questions