[Haskell-cafe] Filtering on data constructors with TH

Evan Martin martine at danga.com
Thu Jun 1 11:25:55 EDT 2006


On 6/1/06, Christophe Poucet <christophe.poucet at gmail.com> wrote:
> That's why
> today I have created an automated derivation for data constructor
> filtering. As I started coding someone mentioned that something similar
> can be done with list comprehensions, so I'm not certain about the scope
> of usefulness, however personally I have found the need for this at
> times.

data T = A Int | B String deriving Show
test1 = [A 3, B "hello", A 5]
test2 = [x | x@(A _) <- test1]

The key here is that pattern match failure in a monad calls fail:
http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Afail
and fail in the List monad is [].


More information about the Haskell-Cafe mailing list