私は赤黒木で遊んでいます:私はGHCiの中で次の文を実行した場合Haskellの導出ショーインスタンス
-- Taken from Okasaki 1999
module RedBlackTree where
--node coloring data
--a node is R (red) or B (black)
data Color = R | B
--tree constructor
--a RBT can be E (empty) or be T (a non empty tree)
data RBT e = E | T Color (RBT e) e (RBT e)
--set operations on tree
type Set a = RBT a
--define an empty set
empty :: Set e
empty = E
--define a member of a set
--Sees if an item of type e is
--in a set if type e elements
member :: (Ord e) => e -> Set e -> Bool
member x E = False
member x (T _ a y b) | x < y = member x a -- if less, go left
| x == y = True
| x > y = member x b -- if more, go right
--tree operations
--Insert an element
insert :: (Ord e) => e -> Set e -> Set e
insert x s = makeBlack (ins s)
where ins E = T R E x E --basically the typical BST insert
ins (T color a y b) | x < y = balance color (ins a) y b
| x == y = T color a y b
| x > y = balance color a y (ins b)
makeBlack (T _ a y b) = T B a y b --inserts a black node
-- balance operations
--case 1:
balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
--case 2:
balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
--case 3:
balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
--case 4:
balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
--generic balancing operation
balance color a x b = T color a x b
は:
> RedBlackTree.insert ('b') (RedBlackTree.T R E ('a') E)
次のエラーメッセージが私に語っありませんSet Char
のためのショーのインスタンス:
<interactive>:116:1:
No instance for (Show (Set Char)) arising from a use of `print'
Possible fix: add an instance declaration for (Show (Set Char))
In a stmt of an interactive GHCi command: print it
私は木がを呼び出すことであるために動作していますここで、...
は前に実行されたステートメントです。戻り値はTrue
です。私はこの問題に関する他のSOの投稿を読んできましたが、それらの解決策(例:Haskell: Deriving Show for custom type)は動作しません。例えば
、添加することにより、私は:l
を使用してロードしようとする
instance Show Set where:
show (Set Char) = show Char
Iは、次のエラーメッセージを表示します
:L赤 - 黒tree.hsを [1の1]コンパイルRedBlackTree(解釈赤 - 黒tree.hs)
red-black-tree.hs:54:11: Not in scope: data constructor `Set'
red-black-tree.hs:54:15: Not in scope: data constructor `Char'
red-black-tree.hs:54:28: Not in scope: data constructor `Char'
Failed, modules loaded: none.
私がやろうとしていることにはいくつか問題があると思いますが、利用可能なドキュメントからそれを理解できないようです。
'data tree a = ... deriving(Show)' – bheklilr
'data RBT ... 'の最後に' deriving(Show) 'を追加すると、次のエラーメッセージが表示されます。色) データ型宣言の 'deriving'句から生成されたもの – Joe
'data(Color)= R |の末尾に' deriving(Show) B ' – nponeccop