Or-Patterns in Haskell yet?

0 views
Skip to first unread message

Phil Tomson

unread,
May 9, 2008, 7:13:07 PM5/9/08
to pdx...@googlegroups.com
I was reading through Okasaki's paper "Red-Black Trees in a Functional
Setting" (recommended ) today and translating the code from Haskell to
OCaml as I went along.

I noticed that when he got to the balance function he says something
to the effect of "If Haskell had or-patterns this is how you could
write this function more concisely". I noticed that this paper was
written in '93 so I figured that maybe Haskell has Or-patterns now,
but a google search didn't uncover anything conclusive - there was
some 2005 post by Simon Peyton-Jones saying that he'd add them if
anyone came up with an implementation. So has anyone come up with an
implementation?

This is how the balance function came out in OCaml:

let rec balance t = match t with
T(B, T(R, T(R, a,x,b), y, c), z, d) | T( B, T(R,a,x,T(R,b,y,c)),z,d) |
T(B,a,x, T(R,T(R,b,y,c),z,d) ) | T(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))
| _ -> t ;;

Phil

Iavor Diatchki

unread,
May 9, 2008, 8:51:03 PM5/9/08
to pdx...@googlegroups.com
Hello,
Indeed, there are no or-patterns in Haskell. They are not hard to
implement but I don't think that there has been much demand for them
(and Haskell is a rather large language already :-). By the way, you
should send this email to one of the Haskell lists
(haskel...@haskell.org seems appropriate). Your example is quite
nice, and it may turn out that other people would find such
functionality useful too.
-Iavor
Reply all
Reply to author
Forward
0 new messages