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

Haskell 错误3例;如何修复它们?,haskell,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)]) fernand

错误:

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)
问题就在这里

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
您忘记将“cantidad”函数应用于
politico
。“无实例”部分

。。。这意味着您无法测试函数,如cantidaddeleccionesalasquesepresento,以获得(in)相等性。这应该起作用:

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

注意:您现在可能不需要处理这个问题(最好每次只执行一步),但是使用
length
两次来计算比率效率相当低,因为您必须至少运行列表两次(三次计算过滤次数)。更好的解决方案是使用折叠(例如
Data.List
中的
foldl'
)一次完成所有工作。

GHC编译错误的好处是它们实际上(大部分)非常有助于您完成工作

让我们看一下实例,但首先从以下内容开始:

coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico  
    | cantidadDeEleccionesALasQueSePresento politico /= 0 =
        cantidadDeVecesQueGano politico / cantidadDeEleccionesALasQueSePresento politico
    | otherwise = 0
这是GHC告诉你的,在最后一个表达式中有一些问题。它要求你在
Eq(Politico->Int)
上做点什么。这是调试时最好的快速阅读线索。 该类型签名是否应该导致该表达式出现错误

如果你看一下
cantidaddeleccionesalasquesepresento
,它实际上是一个类型签名为
Politico->Int
的函数

这显然意味着你不能对它执行
/=0
操作。因此,立即的纠正措施是将
cantidadeeleccionesalasquepresento
更改为
cantidadeeleccionesalasquepresento politico

实例业务

将Haskell视为类型、类型类和函数会有所帮助。你在语言中有价值观。每个值都是某种类型的。每种类型都可以派生一些类型类。类型类在某种程度上类似于该类型的值所具有的某些属性
Eq Show Ord
是表示相等、“打印能力”和排序的属性
Int String
是两种类型,其值服从该属性。您可以创建自己的类型,并确保它们遵守这些类型类(通常只需添加
derivate
语句)

/=
是一个对具有
Eq
类的对象进行操作的函数。 GHC告诉您的是,它遇到了一个不具有Equality属性的值。它告诉您该值属于
Politico->Int
类型。这种类型表示函数。现在这不能等于0,这是有道理的

不要被错误消息的长度所困扰。最重要的几行只是告诉你行号和冒犯性表达的前几行。其余部分将帮助您快速定位精确的父表达式

coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico  
    | cantidadDeEleccionesALasQueSePresento politico /= 0 =
        cantidadDeVecesQueGano politico / cantidadDeEleccionesALasQueSePresento politico
    | otherwise = 0
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)