[Haskell-cafe] Design question

haskell at kudling.de haskell at kudling.de
Wed Dec 16 11:40:52 EST 2009


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

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


Now i have two questions:

1) Is the way i define and use "SubSet", the only/valid way to define subsets?

2) What's the best way to make "doSomethingElse" polymorphic?

There's the tedious way:

doSomethingElse (SubSetA x) = doSomethingElse x
doSomethingElse (SubSetB x) = doSomethingElse x

or i could make SubSet and A,B,C instances of a type class with the function
"doSomethingElse".
But i have several different subsets so the type class aproach feels like
overkill for these helper structures.


Whatever my approach is, it does not feel right. Can you help me step back and
get the picture?

Thanks,
Lenny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091216/1348fc28/attachment.html


More information about the Haskell-Cafe mailing list