[Haskell-cafe] Generic data constructor in pattern?

Neil Mitchell ndmitchell at gmail.com
Tue Aug 21 11:24:13 EDT 2007


Hi Peter,

>       liftV1 f (V x y) = V (f x) (f y)
>         liftV2 f (V x1 y1) (V x2 y2) = V (f x1 x2) (f y1 y2)
>
>         liftM1 f (M x y) = M (f x) (f y)
>         liftM2 f (M x1 y1) (M x2 y2) = M (f x1 x2) (f y1 y2)

> Both pairs of lift functions have almost identical implementations. Can I
> merge these somehow?

Using the Uniplate library the first already has a name, its called "descend".

The second does not, but could be implemented in Uniplate if you wanted.

descend2 :: Biplate a b => (b -> b -> b) -> a -> a -> a
descend2 f a b = a2 (zipWith f as bs)
   where
      (as, a2) = uniplate a
      (bs, b2) = uniplate b

For full details see the website: http://www-users.cs.york.ac.uk/~ndm/uniplate/

Thanks

Neil


More information about the Haskell-Cafe mailing list