gpt4 book ai didi

Haskell 没有 Show 实例

转载 作者:行者123 更新时间:2023-12-04 21:57:33 24 4
gpt4 key购买 nike

这是我的代码

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

谁可以给我解释一下这个?我该如何解决?

最佳答案

简而言之,该函数计算正确 - 问题是由在 ghci 中对其进行评估引起的.

您的函数调用返回 Ingredientes 类型的值这是 [(String, Int, [Efectos])] 的类型同义词. ghci尝试将返回值打印到控制台,因此它尝试调用 show在上面。但是Efectos是函数的类型同义词。 ghci告诉你它不知道如何显示一个函数,因为它不是类型类 Show 的成员:No instance for (Show ((Int, Int, Int) -> (Int, Int, Int))) .

它可能不应该让您担心 - 通过评估 ghci 中的任何函数,您会得到类似的错误。 .

关于Haskell 没有 Show 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23504950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com