sto giocando con un albero rosso-nero:Haskell derivanti Visualizza grado
-- 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
Se eseguo la seguente dichiarazione in GHCi:
> RedBlackTree.insert ('b') (RedBlackTree.T R E ('a') E)
Il seguente messaggio di errore mi dice di non v'è un istanza di spettacolo per 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
so che l'albero sta funzionando perché chiamando 01.232.dove ...
è l'istruzione precedentemente eseguita, il valore restituito è True
. Ho letto altri post SO su questo problema ma le soluzioni trovate per loro (ad es. Haskell: Deriving Show for custom type) non funzionano.
Ad esempio, aggiungendo:
instance Show Set where:
show (Set Char) = show Char
ricevo il seguente messaggio di errore quando provo a caricare utilizzando :l
:
: L rosso-nero-tree.hs [1 di 1] RedBlackTree compilazione (rosso-nero-tree.hs, interpretati)
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.
Penso che ci siano alcuni problemi in corso con quello che sto cercando di fare ma non riesco a capirlo dalla documentazione disponibile.
Basta usare 'data Tree a = ... derivando (Mostra)' – bheklilr
Aggiungendo 'derivando (Mostra)' alla fine di 'dati RBT ...' mi dà il seguente messaggio di errore: 'Nessuna istanza per (Mostra Colore) derivante dalla clausola 'derivante' di una dichiarazione del tipo di dati ' – Joe
Basta aggiungere' derivante (Mostra) 'alla fine di' dati Colore = R | B' – nponeccop