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

Krzysztof Skrzętnicki gtener at gmail.com
Mon Oct 19 05:05:20 EDT 2009


On Mon, Oct 19, 2009 at 10:49, 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.

You can do that with arrows like this:

Prelude Control.Arrow> let rangeA x y = ((>x) &&& (<y)) >>> (uncurry (&&))
Prelude Control.Arrow> :t rangeA
rangeA :: (Ord a) => a -> a -> a -> Bool
Prelude Control.Arrow> filter (rangeA 4 8) [0..10]
[5,6,7]

Best regards

Krzysztof Skrzętnicki


More information about the Beginners mailing list