[Haskell-cafe] Design question

Luke Palmer lrpalmer at gmail.com
Wed Dec 16 16:15:03 EST 2009


On Wed, Dec 16, 2009 at 9:40 AM, haskell at kudling.de <haskell at kudling.de> wrote:
> Hi,
>
> i am not quite sure how to do this in the most elegant way:
>
> I have some data structures:
>
> data A = A Double
> data B = B Double
> data C = C Double
> ...
>
> and i want to allow only a subset in another data structure, so i did
> something like this:
>
>     data SubSet = SubSetA A | SubSetC C

I think you might be looking for too much sugar.  I don't know much
about your problem, but I would use approximately your approach and be
straightforward:

type SubSet = Either A C

> and use it in Foo:
>
>     data Foo = Foo [SubSet]
>
> No i want to perform a polymorphic operation on the contents of A,B,C, e.g.
>
> doSomething :: Foo -> [Double]
> doSomething (Foo s) = map doSomethingElse s

doSomething (Foo s) = map (doSomethingWithA ||| doSomethingWithC) s

(||| is from Control.Arrow)

If that gets too complicated, you can build the "doSomething"
functions in a type-directed way using typeclasses:

class DoSomething a where
    doSomething :: a -> Double

instance DoSomething A where ...
instance DoSomething B where ...
instance DoSomething C where ...

instance (DoSomething a, DoSomething b) => DoSomething (Either a b) where
    doSomething = doSomething ||| doSomething

Luke


More information about the Haskell-Cafe mailing list