[Haskell-cafe] A convenient way to deal with conditional function composition?

Maxime Henrion mux at FreeBSD.org
Tue Apr 10 09:14:49 EDT 2007


	Hello all,


I have found myself writing instances of Show for some types of
mine, and I did so by defining the showsPrec function, for performance
reasons.  I ended up with code that I find quite inelegant.  Here's
an example:

data Move = Move {
                 movePiece     :: PieceType,
                 moveFile      :: Maybe File,
                 moveTarget    :: Square,
                 moveIsCapture :: Bool
                 --movePromotion :: Maybe PieceType
               }
  deriving (Eq)

instance Show Move where
  showsPrec _
    Move {
      movePiece     = p,
      moveFile      = f,
      moveTarget    = s,
      moveIsCapture = c
    } = (if p /= Pawn then shows p else id) .
        (maybe id shows f) .
        (if c then ('x':) else id) .
        shows s

I considered writing a conditional composiion combinator to avoid all
the 'if foo then f else id' code.  Something looking like this:

f .? True  g = f . g
f .? False g = f

I'm not sure this is the best approach though, and I would be happy
to hear about your suggestions for improving the style of this code,
or any other comment that you think is appropriate.

Thanks,
Maxime


More information about the Haskell-Cafe mailing list