3个实例错误;如何修复它们?

时间:2014-05-10 19:33:19

标签: haskell

type Politico = (String, Int, String, Anteriores)
type Anteriores = [(Int, Bool)]
type Distrito = (String, Int, [String])
type Encuestador = Distrito

alnarvin = ("Raul De Alnarvín", 45, "CPU",[(2003, False), (1999, False), (2009, True)])
fernando = ("Elisa Fernando", 56, "PNG",[ (2003, True), (2005, True), ( 2007, False)] )
rodriguezSolana = ("Ernesto Rodriguez Solana", 49, "FPS", [(2009,True)])
altamirano = ("Daniel Altamiarno", 60, "POO",[ (2003, False), (2005, False), ( 2007, False) ])
carcaman = ("Antonio Francisco Carcaman", 90, "JPG",[(1956, True), (1960, True), (1980, True), (1995, False)])


--PARTE A. JUBILACIONES

coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico  
 |(cantidadDeEleccionesALasQueSePresento /= 0) = (cantidadDeVecesQueGano politico) / (cantidadDeEleccionesALasQueSePresento politico)
 |otherwise = 0

-- FUNCIONES AUXILIARES

eleccionesAnteriores :: Politico -> Anteriores
eleccionesAnteriores (_,_,_, eleccionesAnteriores) = eleccionesAnteriores

cantidadDeEleccionesALasQueSePresento :: Politico -> Int
cantidadDeEleccionesALasQueSePresento politico = (length.eleccionesAnteriores) politico

cantidadDeVecesQueGano :: Politico -> Int
cantidadDeVecesQueGano politico = length (filter snd (map eleccionesAnteriores politico))

错误:

tp haskell.hs:9:49:
No instance for (Eq (Politico -> Int))
  arising from a use of `/='
Possible fix:
  add an instance declaration for (Eq (Politico -> Int))
In the expression: (cantidadDeEleccionesALasQueSePresento /= 0)
In a stmt of a pattern guard for
               an equation for `coeficienteVictorias':
  (cantidadDeEleccionesALasQueSePresento /= 0)
In an equation for `coeficienteVictorias':
    coeficienteVictorias politico
      | (cantidadDeEleccionesALasQueSePresento /= 0)
      = div
          (cantidadDeVecesQueGano politico)
          (cantidadDeEleccionesALasQueSePresento politico)
      | otherwise = 0

tp haskell.hs:9:52:
No instance for (Num (Politico -> Int))

  arising from the literal `0'
Possible fix:
  add an instance declaration for (Num (Politico -> Int))
In the second argument of `(/=)', namely `0'
In the expression: (cantidadDeEleccionesALasQueSePresento /= 0)
In a stmt of a pattern guard for
               an equation for `coeficienteVictorias':
  (cantidadDeEleccionesALasQueSePresento /= 0)

2 个答案:

答案 0 :(得分:1)

正如第一个错误的后半部分所说,

In the expression: (cantidadDeEleccionesALasQueSePresento /= 0)
In a stmt of a pattern guard for
               an equation for `coeficienteVictorias':
  (cantidadDeEleccionesALasQueSePresento /= 0)
In an equation for `coeficienteVictorias':
    coeficienteVictorias politico
      | (cantidadDeEleccionesALasQueSePresento /= 0)
      = div
          (cantidadDeVecesQueGano politico)
          (cantidadDeEleccionesALasQueSePresento politico)
      | otherwise = 0

问题出在

(cantidadDeEleccionesALasQueSePresento /= 0)

你忘了申请" cantidad"函数politico。 "没有实例"部分...

No instance for (Eq (Politico -> Int))
  arising from a use of `/='

...意味着你不能测试一个函数,比如cantidadDeEleccionesALasQueSePresento,以便(in)相等。这应该有效:

coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico  
    | cantidadDeEleccionesALasQueSePresento politico /= 0 =
        cantidadDeVecesQueGano politico / cantidadDeEleccionesALasQueSePresento politico
    | otherwise = 0

注意:您现在可能不需要处理(最好每次都执行一步),但使用length两次计算比率效率相当低,因为您必须至少两次运行列表(三次计算过滤)。更好的解决方案是使用折叠(例如来自foldl'的{​​{1}})来一次性完成所有操作。

答案 1 :(得分:0)

GHC编译错误的好处在于,他们实际上(大部分)非常善于帮助您完成工作。

让我们稍微看一下实例,但首先从:

开始
No instance for (Eq (Politico -> Int))
  arising from a use of `/='
Possible fix:
  add an instance declaration for (Eq (Politico -> Int))
In the expression: (cantidadDeEleccionesALasQueSePresento /= 0)

GHC告诉你,最后一个表达中存在一些问题。它要求你在Eq (Politico -> Int)上做点什么。当你进行调试时,这是最好的快速读取线索。 该类型签名是否应该导致该表达式的错误?

如果您查看cantidadDeEleccionesALasQueSePresento,它实际上是一个Politico -> Int作为其类型签名的函数。

这显然意味着您无法执行/= 0操作。因此,立即更正是将cantidadDeEleccionesALasQueSePresento更改为cantidadDeEleccionesALasQueSePresento politico

instances业务

将Haskell视为类型,类型类和函数是有帮助的。你有语言的价值观。每个值都是某种类型。每种类型都可以派生出一些类型。类型类似于该类型的值具有的某些属性。 Eq Show Ord是表示平等,' Print-ablity'}的属性。和订购。 Int String是两种类型,其值遵循该属性。您可以创建自己的类型并确保它们遵守这些类型类(通常只需添加derive语句)。

/=是一个对具有Eq类的事物进行操作的函数。 GHC告诉你的是,它遇到了一个没有Equality属性的价值。它告诉您此值属于Politico -> Int类型。那种类型表示一种功能。有意义的是,现在不能将其等同于0。

不要因为错误消息的长度而陷入困境。最重要的几行是告诉你行号和违规表达的前几行。其余的是帮助您快速找到确切的父表达式。