繁体   English   中英

无法将预期类型'Integer - > t'与实际类型'Bool'匹配

[英]Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’

在哈斯克尔,

这很好用: (mod 9) 7 它给出了预期的结果:当9除以7( 2 )时的余数。

同样,这也有效: (mod 9) 9 它返回0

这让我认为(mod 9 == 0) 9应该返回True 然而,事实并非如此:它反而引发了错误。

错误:

<interactive>:62:1: error:
    • Couldn't match expected type ‘Integer -> t’
                  with actual type ‘Bool’
    • The function ‘mod 9 == 0’ is applied to one argument,
      but its type ‘Bool’ has none
      In the expression: (mod 9 == 0) 9
      In an equation for ‘it’: it = (mod 9 == 0) 9
    • Relevant bindings include it :: t (bound at <interactive>:62:1)

请帮我理解为什么(mod 9 == 0) 9不会返回True

PS:我确信在Haskell的背景下我对“返回”的使用是有缺陷的。 但是,我刚刚开始,所以请原谅。 (如果你能纠正我,那将是很好的,如果我确实是错的。)

正如我在评论中提到的,看起来你希望mod 9 == 0是一个接受参数的函数,将它传递给mod 9 ,然后返回比较结果。 你可以编写这样的表达式,但它有点复杂。

>>> ((== 0) . (mod 9)) 9
True

在这里, (== 0) . (mod 9) (== 0) . (mod 9)是两个函数的组合, (== 0)mod 9 组合函数接受其参数,将mod 9应用于它,然后将(== 0)应用于结果。 (其中(== 0)\\x -> x == 0的简短形式。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM