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.
<br><br>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:
<br><br>data MassTime = MassTime {<br> ...<br> selectParticipants :: (Participants -> Maybe Participants), -- Picks participants for this mass<br> ... } <br><br>Each mass has particular requirements. Notice the 'selectParticipants' function does not take a mass definition as an argument.
<br><br>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:<br><br> makeMass :: Day -> MassTime<br> makeMass day =<br> mass
<br> where<br> -- Use trick here to capture mass defined and pass to our selection function.<br> mass = MassTime ... (weekendMassSelector mass) ...<br><br>'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:
<br><br> weekendMassSelector :: MassTime -> Participants -> Maybe Participants<br><br>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.
<br> <br>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? :)
<br><br>Justin<br>