简体   繁体   English

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

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

In Haskell, 在哈斯克尔,

This works perfectly fine: (mod 9) 7 . 这很好用: (mod 9) 7 It gives the expected result: remainder when 9 is divided by 7 ( 2 ). 它给出了预期的结果:当9除以7( 2 )时的余数。

Similarly, this works too: (mod 9) 9 . 同样,这也有效: (mod 9) 9 It returns 0 . 它返回0

This led me to think that (mod 9 == 0) 9 should return True . 这让我认为(mod 9 == 0) 9应该返回True However, that hasn't been the case: it threw up an error instead. 然而,事实并非如此:它反而引发了错误。

THE ERROR: 错误:

<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)

Please help me understand why (mod 9 == 0) 9 wouldn't return True . 请帮我理解为什么(mod 9 == 0) 9不会返回True

PS: I'm convinced that my usage of "return" in Haskell's context is flawed. PS:我确信在Haskell的背景下我对“返回”的使用是有缺陷的。 However, I am just starting out, so please excuse me. 但是,我刚刚开始,所以请原谅。 (Would be nice if you could correct me if I am, indeed, wrong.) (如果你能纠正我,那将是很好的,如果我确实是错的。)

As I mentioned in a comment, it appears that you expect mod 9 == 0 to be a function that takes an argument, passes it to mod 9 , then returns the result of the comparison. 正如我在评论中提到的,看起来你希望mod 9 == 0是一个接受参数的函数,将它传递给mod 9 ,然后返回比较结果。 You can write such an expression, but it's a little more complicated. 你可以编写这样的表达式,但它有点复杂。

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

Here, (== 0) . (mod 9) 在这里, (== 0) . (mod 9) (== 0) . (mod 9) is the composition of two functions, (== 0) and mod 9 . (== 0) . (mod 9)是两个函数的组合, (== 0)mod 9 The composed function takes its argument, applies mod 9 to it, then applies (== 0) to the result. 组合函数接受其参数,将mod 9应用于它,然后将(== 0)应用于结果。 (Where (== 0) is a short form for \\x -> x == 0 .) (其中(== 0)\\x -> x == 0的简短形式。)

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

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