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)发布于 2014-05-11 03:53:20
正如第一个错误的后半部分所说,
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,是否相等。这应该是可行的:
coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico
| cantidadDeEleccionesALasQueSePresento politico /= 0 =
cantidadDeVecesQueGano politico / cantidadDeEleccionesALasQueSePresento politico
| otherwise = 0注:您现在可能不需要处理这个问题(每次执行一个步骤会更好),但是使用length计算比率是相当低效的,因为您必须至少运行列表两次(计算过滤三次)。一个更好的解决方案是使用一个文件夹(比如Data.List的foldl' )一次完成所有的操作。
发布于 2014-05-11 04:17:34
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是表示相等、“打印能力”和排序的属性。Int String是两种类型,它们的值都遵循该属性。您可以创建自己的类型,并确保它们遵循这些类型类(通常只需添加一条derive语句)。
/=是一个对具有Eq类的对象进行操作的函数。GHC告诉您的是,它遇到了一个不具有相等属性的值。它告诉你这个值是Politico -> Int类型的。这种类型表示一个函数。现在这不能等同于0,这是有道理的。
不要被错误消息的长度所困扰。最重要的几行是告诉您行号和令人不快的表达式的前几行。其余部分将帮助您快速找到确切的父表达式。
https://stackoverflow.com/questions/23585425
复制相似问题