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

Steve Schafer steve at fenestra.com
Tue Dec 5 12:35:13 EST 2006


To: "Creighton Hogg" <wchogg at gmail.com>
Subject: Re: [Haskell-cafe] Picking out elements of a heterogenous list
From: Steve Schafer <steve at fenestra.com>
Date: Tue, 05 Dec 2006 12:33:16 -0500

On Tue, 5 Dec 2006 11:08:07 -0600, you 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<http://haskell.org/haskellwiki/Existential_type>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?

Use filter, passing it a predicate that returns True for Squares and
False otherwise:

 isASquare :: Shape -> Bool
 isASquare (Square _) = True
 isASquare _ = False

 filter isASquare myShapes

Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/


More information about the Haskell-Cafe mailing list