[Haskell-cafe] filter using foldr point-free?

Svein Ove Aas svein.ove at aas.no
Tue Nov 10 06:56:56 EST 2009

On Tue, Nov 10, 2009 at 12:47 PM, damodar kulkarni
<kdamodar2000 at gmail.com> wrote:
> Hi,
> We can define filter using foldr as under:
> filter1 p = foldr (\x xs -> (if (p x) then (x:xs) else xs))  []
> Can we define filter using foldr but in pointfree style?
Pointfree code is (nearly) always possible, but sometimes not very
desirable. The first thing to do is to ask lambdabot for help; in this
case, the answer is.. this:

filter1 =  flip foldr [] . flip flip id . (ap .) . (`ap` (:)) . (((.) . if') .)

if' p a b = if p then a else b

Now, there may be a better way to go about this, but I don't think I'd
bother trying. The pointful variant seems quite readable as-is. :P

Svein Ove Aas

More information about the Haskell-Cafe mailing list