1

在这段代码中

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

我首先解析了«|»

4

1 回答 1

4

守卫的缩进必须比它们所属的函数的名称更进一步,例如:

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

这是因为在 where 之后,您定义了一个(本地)函数neighbour,它也必须遵循布局规则;如果守卫在更左边,它不是定义的延续neighbour。您会在如下所示的文件中收到相同的错误:

  neighbour (p,q)
| b == p   = Just q
于 2013-06-23T21:35:54.347 回答