Extracting values from several data constructors
Ralf Laemmel
Ralf.Laemmel@cwi.nl
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)
where
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
solution.)
For details:
http://www.cs.vu.nl/Strafunski/gmap/
Ralf
Steffen Mazanek wrote:
>
> Hello.
> 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::Test->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,
> Steffen
>
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
--
Ralf Laemmel
VU & CWI, Amsterdam, The Netherlands
http://www.cs.vu.nl/~ralf/
http://www.cwi.nl/~ralf/