0

我有一个功能:

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 正确放置缩进?

谢谢你。

4

2 回答 2

3

一个if总是需要 athen和一个else分支,所以你可能需要if filterLength == 2 then true else false,这相当于filterLength == 2.

于 2011-07-01T06:23:42.660 回答
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 表达式。

于 2011-07-01T06:25:56.593 回答