首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3个实例错误,如何修复?

3个实例错误,如何修复?
EN

Stack Overflow用户
提问于 2014-05-11 03:33:19
回答 2查看 59关注 0票数 0
代码语言:javascript
复制
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))

错误:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2014-05-11 03:53:20

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

代码语言:javascript
复制
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

问题出在

代码语言:javascript
复制
(cantidadDeEleccionesALasQueSePresento /= 0)

您忘记了将"cantidad“函数应用于politico。“无实例”部分...

代码语言:javascript
复制
No instance for (Eq (Politico -> Int))
  arising from a use of `/='

..。意味着你不能测试一个函数,比如cantidadDeEleccionesALasQueSePresento,是否相等。这应该是可行的:

代码语言:javascript
复制
coeficienteVictorias:: Politico -> Int
coeficienteVictorias politico  
    | cantidadDeEleccionesALasQueSePresento politico /= 0 =
        cantidadDeVecesQueGano politico / cantidadDeEleccionesALasQueSePresento politico
    | otherwise = 0

注:您现在可能不需要处理这个问题(每次执行一个步骤会更好),但是使用length计算比率是相当低效的,因为您必须至少运行列表两次(计算过滤三次)。一个更好的解决方案是使用一个文件夹(比如Data.Listfoldl' )一次完成所有的操作。

票数 1
EN

Stack Overflow用户

发布于 2014-05-11 04:17:34

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

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

代码语言:javascript
复制
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,这是有道理的。

不要被错误消息的长度所困扰。最重要的几行是告诉您行号和令人不快的表达式的前几行。其余部分将帮助您快速找到确切的父表达式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23585425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档