[Haskell-cafe] Newbie Question on type constructors

Benjamin Franksen benjamin.franksen at bessy.de
Mon Nov 1 19:43:23 EST 2004


On Monday 01 November 2004 23:48, Ben Rudiak-Gould wrote:
> 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].

Yes, i remember i have seen this technique mentioned before, i believe it was 
in Structure and Interpretation of Computer Programs.

Ben

PS: Lazy K is cool!


More information about the Haskell-Cafe mailing list