How to extract terms of specific data constructor from a list in Haskell

Posted by finnsson on Stack Overflow See other posts from Stack Overflow or by finnsson
Published on 2010-05-08T16:40:07Z Indexed on 2010/05/08 16:48 UTC
Read the original article Hit count: 241

Filed under:

A common problem I got in Haskell is to extract all terms in a list belonging to a specific data constructor and I'm wondering if there are any better ways than the way I'm doing it at the moment.

Let's say you got

data Foo = Bar | Goo

, the list

foos = [Bar, Goo, Bar, Bar, Goo]

and wish to extract all Goos from foos. At the moment I usually do something like

goos = [Goo | Goo <- foos]

and all is well. The problem is when Goo got a bunch of fields and I'm forced to write something like

goos = [Goo a b c d e f | Goo a b c d e f <- foos]

which is far from ideal. How you do usually handle this problem?

© Stack Overflow or respective owner

Related posts about haskell