Haskell让/在哪里和如果缩进

时间:2011-07-01 06:19:46

标签: haskell indentation let

我有一个功能:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList
                       ....

在filterLength之后我想检查filterLength的数量,我试试:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList
                       in if filterLength == 2
                          then true

我收到错误:

    parse error (possibly incorrect indentation)
Failed, modules loaded: none.

如何使用if和in?

正确缩进缩进

谢谢。

2 个答案:

答案 0 :(得分:3)

if始终需要thenelse分支,因此您可能需要if filterLength == 2 then true else false,相当于filterLength == 2

答案 1 :(得分:3)

这将编译:

isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList )
                   in filterLength == 2

main = print $ isSimpleNumber 5

在“deriveList”之后缺少关闭“)”。你也不需要if-then-true表达式。