[Haskell-cafe] Gotta love Haskell - Mass management

Henning Thielemann lemming at henning-thielemann.de
Mon Apr 23 10:47:57 EDT 2007


On Fri, 20 Apr 2007, Justin Bailey wrote:

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

I'd call that
  http://www.haskell.org/hawiki/TyingTheKnot
 However, I wonder if it is clearer to use 'let' for holding the computed
weekendMass value before passing it to MassTime and somewhere else:

let wm = computeWM
in  MassTime {otherData = f wm, weekendMassSelector = wm, ...}


In an imperative language you would certainly write

mass.weekend := bla;
mass.otherData := f(mass.weekend);


More information about the Haskell-Cafe mailing list