[Haskell-cafe] (liftM join .) . mapM

David Menendez dave at zednenem.com
Tue Dec 29 13:01:02 EST 2009


On Tue, Dec 29, 2009 at 12:24 PM, Stephen Tetley
<stephen.tetley at gmail.com> wrote:
> oo is one of of a family of functions I use often to avoid
> sectioning/composing mania. It's known to Raymond Smullyan fans as
> 'blackbird', though I call it oo as a pun on Standard MLs o (which is
> Haskells (.) of course).
>
> -- | Compose an arity 1 function with an arity 2 function.
> -- B1 - blackbird
> oo :: (c -> d) -> (a -> b -> c) -> a -> b -> d
> oo f g = (f .) . g
>
> Extending the arity works quite nicely too:
>
> -- | Compose an arity 1 function with an arity 3 function.
> -- B2 - bunting
> ooo :: (d -> e) -> (a -> b -> c -> d) -> a -> b -> c -> e
> ooo f g = ((f .) .) . g
>
> ... and so on. I've used `oooo` but some how never needed `ooooo`. Due
> to their typographical appearance in infix form, the family name I
> have for them is specs (i.e. glasses, googles...) - `oo`

Why restrict yourself to functions? You can generalize this to
arbitrary stacks of functors.

oo :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
oo = fmap . fmap

ooo :: (Functor f, Functor g, Functor h) => (a -> b) ->  f (g (h a))
-> f (g (h b))
ooo = oo . fmap
etc.

(Unfortunately, the Functor ((->) a) instance is orphaned in
Control.Monad.Instances, at least until some future Haskell revision
finally adds it to the Prelude.)

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list