[Haskell-cafe] Gotta love Haskell

Justin Bailey jgbailey at gmail.com
Fri Apr 20 19:21:09 EDT 2007


My wife has been put in charge of scheduling lectors and ministers at our
church, so of course she needs a Haskell program to do the schedules for
her! While I've been working on it, I wrote something that I thought was a
pretty cool trick.

Each Mass requires a variety of different type and number of participants,
so when defining the "Mass" data structure I put a function in which selects
participants for that type of Mass from a given list:

data MassTime = MassTime {
  ...
  selectParticipants :: (Participants -> Maybe Participants), -- Picks
participants for this mass
  ... }

Each mass has particular requirements. Notice the 'selectParticipants'
function does not take a mass definition as an argument.

Here's the trick I thought was neat. When creating the MassTime value, I
capture it in a closure and bind it to the selector function, as below:

 makeMass :: Day -> MassTime
 makeMass day =
    mass
  where
    -- Use trick here to capture mass defined and pass to our selection
function.
    mass = MassTime ... (weekendMassSelector mass) ...

'weekendMassSelector' is defined elsewhere, and basically knows how to count
up the right number of participants based on the mass definition given. It's
signature is:

  weekendMassSelector :: MassTime -> Participants -> Maybe Participants

So weekendMassSelector gets the MassTime value it needs, before it is even
constructed. Partial evaluation then gives a function with the (Participants
-> Maybe Participants) signature required by the data constructor. I love
Haskell - this just seems too cool.

Since I really doubt this is something new - is this a technique that's
commonly used? Does it have a name? It looks a lot like passing a "this"
argument to a function - have I re-invented OOP encapsulation? :)

Justin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070420/86b7acf5/attachment.htm


More information about the Haskell-Cafe mailing list