내가 레드 - 블랙 트리와 함께 연주하고있다 : 나는 GHCi에서 다음 문을 실행하면하스켈 유도 표시 인스턴스
-- 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
입니다. 이 문제에 대한 다른 게시물을 읽었지 만 찾을 수있는 솔루션 (예 : Haskell: Deriving Show for custom type)이 작동하지 않습니다. 예를 들어, 추가하여
:
나는 다음과 같은 오류 메시지가instance Show Set where:
show (Set Char) = show Char
내가하려고 할 때이 :l
를 사용하여로드하려면
: 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.
,536,
내가 뭘하려고하는지에 관해서 몇 가지 문제가 있다고 생각하지만 사용 가능한 문서에서 그것을 파악할 수없는 것처럼 보입니다.
'data tree a = ... deriving (Show) '을 사용하십시오. – bheklilr
'data RBT ...'의 끝에'deriving (Show) '을 추가하면 다음과 같은 오류 메시지가 나타납니다. Color) 데이터 형식 선언의 'deriving'절에서 발생합니다. – Joe
'data (Color) = R | B' – nponeccop