[Haskell-beginners] Pointfree expeiments
stephen.tetley at gmail.com
Thu Nov 25 04:12:59 EST 2010
On 25 November 2010 01:44, Paul Sargent <psarge at gmail.com> wrote:
> I'm aware that 'ap' is related the liftM, but what is this monad, and why are we working in a monad at all?
> ...and surely this isn't the same as the original code as we now have a different type signature?_______________________________________________
Its the Reader monad.
The Reader monad introduces a "static argument" into the computation.
The code is equivalent to the original - if you want you can declare
it as a binding without generalizing it to Monad:
Prelude Control.Monad> let f = ap (+) id :: Num a => a -> a
Pointfree is presumably introducing it because it can't find
elementary definitions that are only functional types - the
combinators in the Haskell Prelude and Data.Function are perhaps a
little meagre. In one stroke it is a use of the 'w' combinator:
f :: Num a => a -> a
f = w (+)
-- | W combinator - warbler - elementary duplicator.
w :: (r1 -> r1 -> ans) -> r1 -> ans
w f x = f x x
Monadic ap for the Reader monad corresponds to the Starling 's'
combinator, so it is also:
g :: Num a => a -> a
g = starling (+) id
Again, Pointfree doesn't have starling available:
starling :: (r1 -> a -> ans) -> (r1 -> a) -> r1 -> ans
starling f g x = f x (g x)
More information about the Beginners