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

Justin Bailey jgbailey at gmail.com
Wed Dec 6 13:34:51 EST 2006

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

  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

and in the where statement:

            T _ a y b = ins s

Is "T" being redefined, so the "T" called in the first line is a new
function that happens to be called "T" which is defined in the where
statement? If that was the case, I would expect this to work:

    insertSet x s = foo B a y b
      where ins E = foo R E x E
            foo _ a y b = ins s

but it does not. If anyone can explain what's going on here, I'd appreciate
it. Thank you!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20061206/c8155fd9/attachment.htm

More information about the Haskell-Cafe mailing list