在一个数据类型定义中,我怎样才能'name' 相似的引用?
In a data type definition, how can I 'name' similar references?
我可以这样定义一棵树:
data Tree a = Leaf a | Branch (Tree a) (Tree a)
假设我想提醒自己第一棵树是左树,第二棵树是右树。
我试过了
data Tree a = Leaf a | Branch (L (Tree a)) (R(Tree a))
失败了。所以 Haskell 知道 Branch
是构造函数,但不能对 L
或 R
给出此语句做同样的事情。
我试图帮助:
data L a = L a
deriving (Show)
data R a = R a
deriving (Show)
data Tree a =
Leaf a | Branch (L (Tree a)) (R (Tree a))
deriving (Show)
这行得通,但现在我可以有一个 L "whatever"
而不是左分支。比看起来更多的代码我需要提醒自己第一棵树是 'left' 第二棵树是 'right'.
有更好的方法吗?
听起来你想要一张唱片:
data Tree a = Leaf a | Branch { l :: Tree a, r :: Tree a}
那你就可以
Leaf 3
或一个
Branch { l=Leaf 4, r=... }
我可以这样定义一棵树:
data Tree a = Leaf a | Branch (Tree a) (Tree a)
假设我想提醒自己第一棵树是左树,第二棵树是右树。
我试过了
data Tree a = Leaf a | Branch (L (Tree a)) (R(Tree a))
失败了。所以 Haskell 知道 Branch
是构造函数,但不能对 L
或 R
给出此语句做同样的事情。
我试图帮助:
data L a = L a
deriving (Show)
data R a = R a
deriving (Show)
data Tree a =
Leaf a | Branch (L (Tree a)) (R (Tree a))
deriving (Show)
这行得通,但现在我可以有一个 L "whatever"
而不是左分支。比看起来更多的代码我需要提醒自己第一棵树是 'left' 第二棵树是 'right'.
有更好的方法吗?
听起来你想要一张唱片:
data Tree a = Leaf a | Branch { l :: Tree a, r :: Tree a}
那你就可以
Leaf 3
或一个
Branch { l=Leaf 4, r=... }