Higher Order Function Question

Richard Carlsson <>
Fri May 24 11:09:38 CEST 2002


On Fri, 24 May 2002, Raimo Niskanen wrote:

> I cannot come up with anything better than:
> 
> 	lists:filter(fun (Attr) -> not attr:isPk(Attr) end, SomeList)
> 
> or
> 
> 	complement(Fun) -> fun (Attr) -> not Fun(Attr) end.
> 
> 	lists:filter(complement({attr, isPk}), SomeList)
> 	% or
> 	lists:filter(complement(fun isPk/1), SomeList) % within module 'attr'
> 	% or
> 	lists:filter(complement(fun(Attr) -> attr:isPk(Attr) end, SomeList) %
> Very pointless with the complement() function
> 
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> 
> 
> 
> 
> Alex Peake wrote:
> > 
> > Can someone help me with this (no doubt simple) problem.
> > 
> > I try:
> > 
> > 1> attr:isPk(Attr).
> > false
> > 
> > 2> not attr:isPk(Attr).
> > true
> > 
> > 3> not(attr:isPk(Attr)).
> > true
> > 
> > but cannot find a permutation/combination of:
> > 
> > 4> apply({attr, isPk}, [A1]).
> > false
> > 
> > that can include "not" and work.
> > 
> > Actually, I am trying to lists:filter( ...not isPk... SomeList).
> > 
> > The simple is to define notIsPk(Attr), or perhaps lists:filter( fun(Attr) ->
> > .. end  but they are not an abstraction of "complement".
> > 
> > Perhaps I should define a function complement(Fn) that returns a function
> > that is the complement of the function passed?
> > 
> > Perhaps there is already an Erlang way to do this (I hope).
> > 
> > Alex
> 

Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://www.csd.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham




More information about the erlang-questions mailing list