Extracting values from several data constructors
Thu, 24 Apr 2003 13:10:18 +0200
Would you mind to become a bit more generic?
The following works with the gmap combinators.
-- Find an immediate subterm of type y given a subterm of type x
findOne :: (Term x, Term y) => x -> Maybe y
findOne = singleton
. foldl unJust 
. gmapQ (Nothing `mkQ` Just)
unJust l (Just x) = x:l
unJust l Nothing = l
singleton [s] = Just s
singleton _ = Nothing
(That is, we map over all the immediate subterms
to select all entities of type y. Then,
we fold over this list of maybes to reduce
it to a list of y's. Then, we insist on
a singleton list for the sake of an unambiguous
Steffen Mazanek wrote:
> I am sure this question is not new. Even so, please
> give me the solution or point me to an old thread.
> How can I implement something like:
> data Test = T1 Int | T2 Int
> test (_ x) = x
> Or is this impossible? In case of yes, why?
> This would save a lot of case differentiations in
> my application. Or is there an appropriate compiler option?
> Good night,
> Haskell mailing list
VU & CWI, Amsterdam, The Netherlands