gpt4 book ai didi

haskell - Haskell中的实际类型与预期类型错误

转载 作者:行者123 更新时间:2023-12-02 10:44:24 25 4
gpt4 key购买 nike

我在Haskell中声明了这些类型:

data Tree x = Node x (Tree x) (Tree x)
|Leef
deriving Show

data Color = R | N
deriving (Show, Eq)

data TreeRN x = Tree (Color,x)
deriving Show

和这个功能:
equilibre :: TreeRN a -> TreeRN a
equilibre (Node (N,z) (Node (R,y) (Node (R,x) a b) c) d) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,z) (Node (R,x) a (Node (R,y) b c)) d) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,x) a (Node (R,z) (Node (R,y) b c) d)) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,x) a (Node (R,y) b (Node (R,z) c d))) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))

这是我的问题:
Couldn't match expected type ‘TreeRN a’
with actual type ‘Tree (Color, t3)’
Relevant bindings include
d :: Tree (Color, t3) (bound at test.hs:15:53)
c :: Tree (Color, t3) (bound at test.hs:15:51)
z :: t3 (bound at test.hs:15:48)
b :: Tree (Color, t3) (bound at test.hs:15:37)
y :: t3 (bound at test.hs:15:34)
a :: Tree (Color, t3) (bound at test.hs:15:23)
equilibre :: TreeRN a -> TreeRN a (bound at test.hs:12:1)
(Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-relevant-binds)
In the expression:
(Node (R, y) (Node (N, x) a b) (Node (N, z) c d))
In an equation for ‘equilibre’:
equilibre (Node (N, x) a (Node (R, y) b (Node (R, z) c d)))
= (Node (R, y) (Node (N, x) a b) (Node (N, z) c d))
Failed, modules loaded: none.

通常,当我收到此类错误时,我会理解为什么并且可以更正我的功能,但是在这里,我对
Couldn't match expected type ‘TreeRN a’
with actual type ‘Tree (Color, t3)’

虽然我声明的类型TreeRN是相同的(对我而言),所以我需要回答为什么编译器说“t3”,这是什么意思?它真的和Tree(Color,a)不同吗?

最佳答案

您似乎希望TreeRN xTree (Color, x)的同义词。我认为您刚刚使用了错误的关键字-data定义了一种新的代数数据类型。尝试

type TreeRN x = Tree (Color, x)

您还需要删除对类型同义词无效的 deriving子句。

关于haskell - Haskell中的实际类型与预期类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356413/

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