Monday, May 26, 2008

No Or-Patterns in Haskell?

I ran across Chris Okasaki's excellent Red-Black Trees in a Functional Setting article a few weeks back.  Excellent functional take on Red-Black trees - so much easier to understand than the imperative explanations I've seen elsewhere.  Highly recommended.  

The example code in the article is in Haskell.  I've been learning OCaml for several months now and reading an article like this helps me learn a bit of Haskell along the way.   I did a translation of the code in the article into OCaml which lets me compare and contrast the languages.   Here's my OCaml translation:




(* RedBlack trees in OCaml *)
(* see Okasaki paper: Functional Pearls: Red-Black Trees in a Functional
Setting *)
type color = R | B ;;

type 'a tree = E | T of color * 'a tree * 'a * 'a tree ;;

let rec member x t = match t with
E -> false
| T( _, a, y , b) -> ( if x = y then true else
if x > y then (member x b) else
(member x a) ) ;;


let makeBlack node = match node with
E -> node
| T( _, a, y, b) -> T( B, a, y, b) ;;


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 ;;

let rec insert x s =
let rec ins s = match s with
E -> T( R, E, x, E)
| T(c, a, y, b) -> ( if x = y then T(c,a,y,b ) else
if x > y then ( balance (T(c,a,y,(ins b))) ) else
( balance (T(c,(ins a),y,b)) ) ) in
makeBlack( ins s) ;;





Notice that in the balance function there are four patterns which lead to the same result. OCaml handles this nicely without repetition of code. However it seems that the Haskell version requires that the result be repeated for each of these cases as Okasaki notes in his article. His article was written in 1993 and he mentions that in the future Haskell could have or-patterns, but from what I can tell by googling around a bit and asking on the PDX_func mailing list this feature is still not in Haskell. So here's a situation where I definitely can say that I prefer the (OCa)ML solution. I'm sure there are other things to prefer in Haskell, though, like type classes.

Saturday, May 17, 2008

This American Life Explains the Mortgage Meltdown

Not programming related, but This American Life did a great show on the Mortgage Meltdown.  If you've wondered what led to this debacle which will lead to $Trillions of lost wealth in the US (technically speaking, it's not being lost, it was never really there - it was pretend) give this show a listen.

Saturday, May 10, 2008

Yet another programming blog is born

Let's see, I guess this is that part of the interview where the interviewer asks me to tell him/her about myself.  Always an awkward question...

I've been a software developer since the early 90's.  Prior to that I was firmly ensconced in the hardware side of the world: wire wrap, PLDs, FPGAs, ASICs, schematics and all that.  At some point I got the idea that it would be more fun to develop the software used to develop the hardware so I made the switch.  

My paid gig is in EDA (Electronic Design Automation) developing software used to design chips - DSP algorithms in FPGAs, for example.

What do I want to explore in this blog?  Lots of stuff.  My interests are many and varied, maybe even eclectic.  Most recently I've been exploring functional programming.  I'm involved in a local programming group here in Portland dedicated to discussion of functional programming: PDXfunc.  Whereas all the smart kidz seem to be learning Haskell, I decided a while back to go against the tide and learn OCaml.  I'm  a fan of the Pragmatic Programmers and their admonition to learn a new programming language every year - though I my case I'm learning a new one every 3 or 4 years.  So make that "Learn a New Programming Language Every 3 or 4 years" - actually, I think that's pretty reasonable.

I'm also exploring some ideas related to concurrency & parallelism.   How about using FPGAs to accelerate certain types of algorithms?  

My favorite programming language to date?  That would be Ruby.  I was an early adopter.  Picked it up in 2001.  Prior to that my favorite language was Perl, but since I started using Ruby I haven't even wanted to look at any Perl code for many years now. 

Where do I live?  Near Beaverton, Oregon.

What are some of my other hobbies, you ask?  Gardening - the Fava beans are coming along nicely, thanks.  Coral Reef aquaria - I've got a little 12 gallon nano-reef with live corals in my living room.

What operating systems do I use?  Linux and OS X.

...Oh, and I prefer cats.  No dogs here.