简体   繁体   English

无法将预期类型'Integer'与实际类型'm0 Integer'匹配

[英]Couldn't match expected type ‘Integer’ with actual type ‘m0 Integer’

So I'm new to Haskell and I'm writing a basic function which takes 3 integers as arguments and returns how many are equal. 所以我是Haskell的新手,我正在编写一个基本函数,它将3个整数作为参数并返回多少是相等的。

howManyEqual :: Int->Int->Int->Integer

howManyEqual x y z =  
        if x == y && x == z then return 3
        else if x == y && x /= z then return 2
        else if x == z && x /= y then return 2
        else if y == z && y /= x then return 2
        else return 0

But Im getting the following error: 但我得到以下错误:

Prelude> :load ex4.1_2.hs 
[1 of 1] Compiling Main             ( ex4.1_2.hs, interpreted )

ex4.1_2.hs:11:34:
    Couldn't match expected type ‘Integer’
                with actual type ‘m0 Integer’
    In the expression: return 3
    In the expression:
      if x == y && x == z then
          return 3
      else
          if x == y && x /= z then
              return 2
          else
              if x == z && x /= y then
                  return 2
              else
                  if y == z && y /= x then return 2 else return 0

I have same error at return 2 and return 0 as well. 我在return 2有相同的错误,并return 0 What kind of data type is m0 Integer and what do I need to do to fix this? 什么样的数据类型是m0 Integer ,我需要做些什么来解决这个问题? Any help would be appreciated. 任何帮助,将不胜感激。 Cheers!! 干杯!!

Delete all the return keywords: 删除所有return关键字:

howManyEqual :: Int -> Int -> Int -> Integer
howManyEqual x y z =  
  if x == y && x == z then 3
  else if x == y && x /= z then 2
  else if x == z && x /= y then 2
  else if y == z && y /= x then 2
  else 0

In Haskell, return isn't a keyword, but a function with the type Monad m => a -> ma . 在Haskell中, return不是关键字,而是Monad m => a -> ma类型的函数。 It's mainly used from within code blocks in do notation to return values 'wrapped' in a Monad instance. 它主要用于从代码块内do记号,以在返回值“包裹” Monad实例。

The howManyEqual method doesn't return a monadic value, but rather a normal, scalar Integer , so return isn't required. howManyEqual方法不返回howManyEqual值,而是返回正常的标量Integer ,因此不需要return When you use return anyway, the compiler expects the returned value to be part of some Monad instance that it calls m0 in the error message, but since the function has the explicitly declared return type Integer , there's no way to reconcile these. 无论如何使用return ,编译器都希望返回的值是某个Monad实例的一部分,它在错误消息中调用m0 ,但由于该函数具有显式声明的返回类型Integer ,因此无法协调它们。

By the way, you can relax the type declaration to: 顺便说一句,你可以放松类型声明:

howManyEqual :: (Eq a, Num t) => a -> a -> a -> t

Some further off topic information about the haskell syntax: 有关haskell语法的更多主题信息:

There is nothing wrong with using else if in haskell, but it's not common. else if在haskell中使用else if没有任何问题,但这并不常见。 Haskell supports guards | Haskell支持守卫| which is some what equivalent to else if . 这是一些相当于else if However, if x then y else z is mosty used for a binary condition, not to gather several different conditions. 然而, if x then y else z用于二元条件,则不收集几个不同的条件。

| x == y = 5 | x == y = 5 = if x ==y then 5 | x == y = 5 = if x ==y then 5

Therefore your code could be expressed in the following and more preferable way 因此,您的代码可以用以下更好的方式表达

 howManyEqual :: Int -> Int -> Int -> Integer
 howManyEqual x y z   
   | x == y && x == z = 3
   | x == y && x /= z = 2
   | x == z && x /= y = 2
   | y == z && y /= x = 2
   | otherwise        = 0

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

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