Haskell 哈斯凯尔没有表演的例子
这是我的代码Haskell 哈斯凯尔没有表演的例子,haskell,Haskell,这是我的代码 type Niveles = (Int, Int, Int) type Persona = (String, Niveles) type Pocion = (String, [(String, Int, [Efectos])]) type Ingredientes = [(String, Int, [Efectos])] type Efectos = Niveles -> Niveles aplicar3 f (a,b,c) = (f a, f b, f c) i
type Niveles = (Int, Int, Int)
type Persona = (String, Niveles)
type Pocion = (String, [(String, Int, [Efectos])])
type Ingredientes = [(String, Int, [Efectos])]
type Efectos = Niveles -> Niveles
aplicar3 f (a,b,c) = (f a, f b, f c)
invertir3 (a,b,c) = (c,b,a)
fst3 (a,_,_) = a
snd3 (_,b,_) = b
trd3 (_,_,c) = c
personas = [("Harry",(11, 5, 4)), ("Ron",(6,4,6)), ("Hermione",(8,12,2)), ("Draco",(7,9,6))]
f1 (ns,nc,nf) = (ns+1,nc+2,nf+3)
f2 = aplicar3 (max 7)
f3 (ns,nc,nf)
| ns >= 8 = (ns,nc,nf+5)
| otherwise = (ns,nc,nf-3)
misPociones :: [Pocion]
misPociones = [
("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f3])]),
("Multijugos",[("Cuerno de Bicornio en Polvo",10, [invertir3, (\(a,b,c) -> (a,a,c))]),("Sanguijuela hormonal",54,[(aplicar3 (*2)), (\(a,b,c) -> (a,a,c)) ])]),
("Flores de Bach",[("Orquidea Salvaje",8,[f3]), ("Rosita",1,[f1])])]
efectosDePocion pocion = map trd3 (elementos pocion)
elementos :: Pocion -> Ingredientes
elementos (_, ingredientes) = ingredientes
当我尝试使用函数“elementos”时,最后一段代码中有一个问题:
elementos(“Felix Felices”(“Escarabajos Machacados”,52[f1,f2]),(“Ojo de Tigre Sucio”,2[f3]))
我收到一条错误消息:
<interactive>:311:1:
No instance for (Show ((Int, Int, Int) -> (Int, Int, Int)))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show ((Int, Int, Int) -> (Int, Int, Int)))
In a stmt of an interactive GHCi command: print it
:311:1:
没有(Show((Int,Int,Int)->(Int,Int,Int))的实例
因使用“print”而产生的
可能的解决方案:
为添加实例声明
(显示((Int,Int,Int)->(Int,Int,Int)))
在交互式GHCi命令的stmt中:打印它
有人能给我解释一下吗?如何修复它?简言之,函数的计算结果正确-问题是在
ghci
中对其进行计算造成的
函数调用返回类型为Ingredientes
的值,它是[(String,Int,[Efectos])]的类型同义词
ghci
尝试将返回值打印到控制台,因此它尝试在控制台上调用show
。但是Efectos
是函数的类型同义词ghci
告诉您它不知道如何显示函数,因为它不是typeclassShow
的成员:没有(Show((Int,Int,Int)->(Int,Int,Int))的实例。
这可能与您无关-通过计算
ghci
中的任何函数,您可能会遇到类似的错误。请将以下代码添加到.hs文件中
instance Show (a -> b) where
show a= "funcion"
现在,ghci将能够打印“function”(函数)
祝你好运 这段代码解决了这个问题,但是@Benesh的答案肯定更有趣——它告诉我们为什么会发生这种情况。虽然这会解决问题,但请不要这样做。这是一个孤立的实例,它本身就不好,但也完全抛出了它的论点。这有可能破坏现有代码。其他人可能也没有意识到这个实例在范围内,并且对字符串“function”随机出现感到惊讶。如果需要,请使用
newtype Func a b=Func(a->b)
或类似工具,并为Show提供单独的实例。