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

Mon Oct 19 12:42:01 EDT 2009

```This one had me puzzled too - so did a traced through the program below.
<---------------------------------------------------------------->

It was more helpful to think of this as using the ((->) r) instance of
GHC source as:
>    return = const
>    f >>= k = \ r -> k (f r) r

And const just returns its first argument like:
const 1 3 => 1
const "hello" "world" => "hello"

And liftM2 is defined in ./libraries/base/Control/Monad.hs as :
>liftM2  :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
>liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

So a trace of the program goes like this:

(liftM2 (&&) (< 0.5) (> -0.5))
=> do {x1 <- (< 0.5);
x2 <- (> -0.5);
return ((&&) x1 x2)}

=> (< 0.5) >>= \x1
(> -0.5) >>= \x2
return ((&&) x1 x2)

=> \r ->(\x1 ->
(> -0.5) >>= \x2
return ((&&) x1 x2))
((< 0.5) r)
r

=> \r -> (return (> -0.5) >>= \x2
return ((&&) ((< 0.5) r) x2))
r

=> \r -> (\r' -> (\x2 ->
return ((&&) (const (< 0.5) r) x2))
((> -0.5) r')
r')
r
=> \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r
=> \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r
=> \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r
=> \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r

hope this helps,
-deech

On Mon, Oct 19, 2009 at 10:24 AM, Jordan Cooper <nefigah at gmail.com> wrote:

> Whoa... how on earth does this work? How does it interpret the
>
> >
> >
> > Lambda Fu, form 53 - silent reader of truth
> >
> >
> >     filter (liftM2 (&&) (< 0.5) (> -0.5)) xs
> >
> >
> >
> > Regards,
> > apfelmus
> _______________________________________________
> Beginners mailing list