Extracting values from several data constructors
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
In general, this is not possible. Perhaps if you were to supply a type signature, I could understand what you wanted the compiler to do, but without, it's just too hard. What you can do is use record labels. Here, you just name the two Ints the same thing and get what you want:
data Test = T1 { test :: Int } | T2 { test :: Int }
this is more general, in that you can say:
data Shape = Circle { center_x :: Double, center_y :: Double, radius :: Double } | Ellipse { center_x :: Double, center_y :: Double, radius_x :: Double, radius_y :: Double }
and get all the functions for free -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume On 24 Apr 2003, 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
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/
participants (4)
-
Hal Daume III -
Ralf Laemmel -
Steffen Mazanek -
Steffen Mazanek