Friday, March 9, 2007

Haskell hacking notes or Prelude to the chat server

I picked up a couple of haskell programming practices this week. They saved me a bit of time, perhaps someone else will find them useful.

Avoid where. Where is handy, it's nice for short simple declarations. Don't use it for multiple functions, or functions with pattern match. This is of course guideline, do whatever you want, but i had several huge where blocks, and I paid a price for that. In general, I'm going to keep where blocks short and sweet.

Write the type declaration first. Perhaps type in a trivial version, load it, then cut and paste the type out of GHCI. Several times, I could have saved myself hours by figuring out the type up front.

Anyway, that was the good stuff, the rest is just rambling. I'm the fire-aim-fire-aim kind of programmer. I do something, then test. Haskell is great for my style of small incremental changes style of programming. Get a simple program working, then you keep it working by replacing hard coded values with functions. as long as it compiles you're in good shape. This last week i've come up with surprisingly long strings of code that typecheck, and run right off the bat. It's always satisfying to see progress.

There were two fundamental problems holding me back this week. First, I don't think the documentation for thread communication is very good. To be fair, there is good documentation available, but the easy to find stuff isn't the best. When you don't know anything, it's hard to evaluate the quality of documentation. My plan is to write about transforming the threaded server, developed earlier, into a chat server.

The second thing that held me back was my shaky grasp of monads. i was constructing a type like IO(STM (TVar [a])). I should have been constructing a type like IO (TVar [a]). I didn't really understand I was carrying around an unnecessary layer of computation. It's still non-obvious (to me) how to carry around those types, but I'm getting there.

No comments: