[Haskell-cafe] Picking out elements of a heterogenous list

J. Garrett Morris trevion at gmail.com
Tue Dec 5 13:52:41 EST 2006

I generally use the Typeable class for this.  In that example, you'd want:

class Typeable a => Shape_ a

instead of just

class Shape_ a

and then your filter predicate would look like:

isSquare :: Shape -> Bool
isSquare (Shape s) = typeOf s == typeOf square
   where square :: Square ; square = undefined

(warning: I didn't try this code.)

That adds a little overhead (particularly in that everything must now
derive Typeable) but is one of the better solutions I've seen.


On 12/5/06, Creighton Hogg <wchogg at gmail.com> wrote:
> Hi Haskell-ers,
> So I think I understand the idea of creating a heterogenous list using
> typeclasses and existentials, but I don't see how to filter the list
> to retrieve elements of the list that are of only one type.
> More concretely, taking the example here how could we take a list of shapes
> [Shape] and pull out all objects that are Squares?
> I don't see an obvious way this makes sense.
> Is there a way of doing heterogenous lists that would make this possible?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

It is myself I have never met, whose face is pasted on the underside of my mind.

More information about the Haskell-Cafe mailing list