[Haskell-cafe] Newbie Question on type constructors

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Mon Nov 1 17:48:40 EST 2004


Benjamin Franksen wrote:

 >Because, hmmm, isn't it rather *one* destructor with type
 >
 >    destructShape :: Shape -> (Double -> t) -> (Double -> t) -> t
 >
 >where the second and third arguments explain what to do with a Circle 
resp. a
 >Square? So that
 >
 >    case s of
 >        Circle r -> f r
 >        Square l -> g l
 >
 >is another way to write
 >
 >    destructShape s g f
 >

I can't resist pointing out that we don't even need destructShape, nor 
any internal representation of a Shape, because we can make the value 
itself the deconstructor:

    Circle :: Double -> (Double -> t) -> (Double -> t) -> t
    Circle d = \c s -> c d

    Square :: Double -> (Double -> t) -> (Double -> t) -> t
    Square d = \c s -> s d

Every algebraic data type has a natural representation of this form. I 
used this idiom extensively in my Lazy K sample code [1] [2].

-- Ben

[1] http://homepages.cwi.nl/~tromp/cl/lazy-k.html
[2] http://esoteric.sange.fi/essie2/download/lazy-k/eg/



More information about the Haskell-Cafe mailing list