[Haskell-cafe] Point-free style in guards

Claus Reinke claus.reinke at talk21.com
Tue Jul 22 14:25:40 EDT 2008


> outStanza | (isMessage) = outMessage
>            | (isPresence) = outPresence
>            | (isIQ) = outIQ
> 
> Why such a style doesn't work, so I must write ugly code like that:
> 
> outStanza a | (isMessage a) = outMessage a
>            | (isPresence a) = outPresence a
>            | (isIQ a) = outIQ a
> 
> so, guards can't be useful in point-free function definitions in any way

You just have to avoid all those pointless language constructs
that let mere Haskellers deal with points, and define your own:

    import Control.Monad
    import Data.Maybe
    import Control.Arrow((&&&))

    g |= rhs = uncurry (>>) . ((guard . g) &&& (return . rhs))
    a +++ b  = uncurry mplus . (a &&& b)
    (=|)     = (fromJust .)

    outStanza = (=|) (((=="m") |= ("message: "++))
                 +++  ((=="p") |= ("presence: "++))
                 +++  ((=="i") |= ("iq: "++)) )

Sorry about mentioning those Strings, but then the names shouldn't
really mention the points (Stanza/Message/..), either, right? Or have
I missed the point of this exercise?-)

Claus



More information about the Haskell-Cafe mailing list