gpt4 book ai didi

Haskell:为自定义类型派生 Show

转载 作者:行者123 更新时间:2023-12-03 09:06:42 46 4
gpt4 key购买 nike

我有这个类型定义:

data Operace = Op (Int->Int->Int) String (Int->Int->Int) deriving Show

我想将此类型打印到交互式外壳(GHCi)中。所有应该打印的是 String field 。

我试过这个:
instance Show Operace where
show (Op op str inv) = show str

但我仍然不断得到
No instance for (Show (Int -> Int -> Int))
arising from the 'deriving' clause of a data type declaration
Possible fix:
add an instance declaration for (Show (Int -> Int -> Int))
or use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Show Operace)

我不想添加 Show对于 (Int->Int->Int) ,我只想打印字符串。

感谢帮助!

编辑:

为了将来引用,固定版本是:
data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
show (Op _ str _) = str

最佳答案

您所做的实例声明是正确的方法。看来你忘了删除那个有缺陷的 deriving来自原始 data 的条款宣言。

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
show (Op op str inv) = show str

关于Haskell:为自定义类型派生 Show,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6082090/

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