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 ;;
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 (haskell-c...@haskell.org seems appropriate). Your example is quite nice, and it may turn out that other people would find such functionality useful too. -Iavor
On Fri, May 9, 2008 at 4:13 PM, Phil Tomson <philtom...@gmail.com> wrote:
> 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 ;;