[Haskell-cafe] Re: Would someone explain this code to me?

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Wed Dec 6 14:33:51 EST 2006


"Justin Bailey" <jgbailey at gmail.com> writes:

> I'm reading Chris Okasaki's "Purely Functional Data Structures", and some
> of  his Haskell  is confusing me. He defines the type Color and RedBlackSet
> as:
> 
>   data Color = R | B
>   data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a)
> 
> and then later he defines a function insertSet:
> 
>     insertSet x s = T B a y b
>       where ins E = T R E x E
> ...
>             T _ a y b = ins s
> 
> What I don't understand is his use of the "T" constructor, both at
> 
>     insertSet x s = T B a y b

Here it creates a new RedBlackSet

> and in the where statement:
> 
>             T _ a y b = ins s

Here it's a pattern match. So if ins s returns (T x a' y'
b'), then a = a'; y = y'; b = b' are used in the expresion
covered by the where clause.
 
-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list