Say I have a mean
function defined like so:
mean xs = sum xs / (fromIntegral $ length xs)
but I want it in some tacit form, like this:
mean = sum / (fromIntegral . length)
Is there a built-in Haskell way to do something along these lines without having to build up my own tacit
function (something like this):
tacit :: (a -> b -> c) -> (d -> a) -> (d -> b) -> d -> c
tacit a b c i = a (b i) (c i)
In this form, the function looks like this:
mean = tacit (/) sum (fromIntegral . length)
but it feels like there might be a way to avoid having to use an explicit function such as this. I'm just wondering; is there some way to do this that is built in to Haskell?
Yes, your
tacit
function isliftM2
in the(->) r
monad (liftM2
is inControl.Monad
, and the function instance of Monad is inControl.Monad.Instances
).I found this using the
pointfree
program (you can install it viacabal install pointfree
), invoked as:(in a Unix terminal)
Applicative functors work pretty well here.