On Sat, 31 Dec 2011 08:50:05 -0700, Stephen Tetley <stephen.tet...@gmail.com> wrote:

Maybe you want a deconstructor (sometime called an eliminator)?

deconsVar :: (Maybe Int -> a) -> (Maybe String -> a) -> Var -> a
deconsVar f g (V1 a) = f a
deconsVar f g (V2 b) = g b

That works and has the advantage of allowing a single deconstructor definition that can be reused in multiple places, but it requires me to add an argument for each wrapped type, which causes some ripple effect if the type changes.

Also, if that argument is parametric over the possible inputs (as asserted by a class restriction) then it starts to get a bit tedious:

main = putStrLn . deconsVar elemStr elemStr $ test

If I find myself adding a V3 and a V4 to my datatype in the future then that would change to:

main = putStrLn . deconsVar elemStr elemStr elemStr elemStr $ test

I was hoping to find a way that would let the functor argument retain its parametricity and therefore not need such repetition.

-Kevin

--
-KQ

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to