[Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5

Daniel Fischer daniel.is.fischer at web.de
Mon Oct 19 11:58:25 EDT 2009


Am Montag 19 Oktober 2009 17:24:12 schrieb Jordan Cooper:
> Whoa... how on earth does this work? How does it interpret the
> sections as Reader monads?

liftM2 (&&) :: (Monad m) => m Bool -> m Bool -> m Bool

So for (liftM2 (&&) (< 0.5) (> -0.5)) to be well typed, we must have
(< 0.5) :: m Bool
for some Monad m (same for (> -0.5)).

Now (< 0.5) :: Fractional a => a -> Bool, hence m must be ((->) a) for some a in the 
Fractional class.

Reader r a is just (r -> a) wrapped in a newtype, so (r -> a) 'is' the reader monad.

To use it, we must import Control.Monad for liftM2 and some module which brings the

instance Monad ((->) r)

into scope. That could be Control.Monad.Instances or, appropriately, Control.Monad.Reader.

>
> > That's a job for the reader monad.
> >
> >
> > Lambda Fu, form 53 - silent reader of truth
> >
> >     import Control.Monad
> >     import Control.Monad.Reader
> >
> >     filter (liftM2 (&&) (< 0.5) (> -0.5)) xs
> >
> >
> >
> > Regards,
> > apfelmus
>




More information about the Beginners mailing list