Haskell 让/在哪里以及是否缩进

发布于 2024-11-17 18:55:43 字数 757 浏览 3 评论 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正确放置缩进?

谢谢。

I have a function:

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

After filterLength i want to check how much filterLength, i try:

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

I get error:

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

How can i put indentation correctly with if and in?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一念一轮回 2024-11-24 18:55:43

这将编译:

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 表达式。

This will compile:

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

There was a missing closing ")" after "deriveList". You don't need an if-then-true expression, also.

堇色安年 2024-11-24 18:55:43

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

An if needs always both a then and an else branch, so you probably need if filterLength == 2 then true else false, which is equivalent to filterLength == 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文