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

Daniel Fischer daniel.is.fischer at web.de
Fri Oct 23 11:23:23 EDT 2009


Am Freitag 23 Oktober 2009 16:25:57 schrieb pl:
>    filter ((<=0.5) . abs) xs

Would be filter ((< 0.5) . abs). Yes, it's fine.

However, if the range is not symmetrical about 0, this is not so nice anymore:

cap lo hi = filter (\x -> lo < x && x < hi)

cap lo hi = filter (liftM2 (&&) (lo <) (< hi))

cap lo hi = filter ((&&) <$> (lo <) <*> (< hi))

cap lo hi = filter ((< halflength) . abs . subtract mid)
      where
        mid = (lo+hi)/2
        halflength = (hi-lo)/2

>
> On Mon, Oct 19, 2009 at 10:49 AM, Michael Mossey <mpm at alumni.caltech.edu>wrote:
> > Is there a nifty way to write
> >
> > filter (\x -> x < 0.5 && x > -0.5) xs
> >
> > without explicitly using x?
> >
> > Maybe arrows? I have a vague understanding that arrows can "send" an
> > argument to more than one computation.
> >
> > -Mike



More information about the Beginners mailing list