我有一个功能:
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?
正确缩进缩进谢谢。
答案 0 :(得分:3)
if
始终需要then
和else
分支,因此您可能需要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表达式。