Algebraic data types: Difference between revisions

(remove indent and) align balance's patterns in columns for clarity
(Added Tcl implementation)
((remove indent and) align balance's patterns in columns for clarity)
Line 51:
=={{header|Haskell}}==
 
<lang haskell>data Color = R | B
data ColorTree a = RE | BT Color (Tree a) a (Tree a)
 
data Tree a = E | T Color (Tree a) a (Tree a)
balance :: Color -> Tree a -> a -> Tree a -> Tree a
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)
balance :: Color -> Tree a -> a -> Tree a -> Tree a
balance B (T R (Ta R a x (T R b) y c)) z d = T R (T B a x b) y (T B c z d)
balance B (Ta R a x (T R (T R b y c)) z d ) = T R (T B a x b) y (T B c z d)
balance B a x (T R x (T R b y (T R c) z d)) = T R (T B a x b) y (T B c z d)
balance Bcol a x (T R b y (T R c z d)) = T R (T Bcol a x b) y (T B c z d)
 
balance col a x b = T col a x b
datainsert Tree:: Ord a => Ea |-> T Color (Tree a) a-> (Tree a)
insert ::x Ord as => aT -> TreeB a ->y Treeb awhere
insert x sins E = T BR aE y bx whereE
ins Es@(T col a y b) = T R E x E
ins s@(T| x < y = balance col (ins a) y b)
| x <> y = balance col (ins a) y (ins b)
| x > y otherwise = balance col a y (ins b)s
T _ a | otherwisey b = ins s</lang>
T _ a y b = ins s
</lang>
 
=={{header|OCaml}}==