Higher Order Function Question

Håkan Stenholm hakan.stenholm@REDACTED
Sun May 26 23:35:40 CEST 2002


On lördag, maj 25, 2002, at 06:08 , Alex Peake wrote:

> Perhaps I could go back a step, to my orginal problem -- obviously not
> stated clearly:
>
> I was struggling to use "filter" with the "complement of" a predicate 
> (in my
> case not attr:isPk).
>
> In explaining the problem, it came out that in my code I wrote things 
> like:
>
> lists:map({attr,varName}, rel:attrList(rel:findNamed(ng,"Member"))).
>
> I was informed that this is "not elegant" and "inefficient" because it 
> "is
> the old classic version of how to call a fun".
>
> So I asked for how to do this the "modern Erlang" way. I pointed out 
> that:
>
> lists:map(fun(Arg) -> attr:varName(Arg) end,
> rel:attrList(rel:findNamed(ng,"Member"))).
>
> is more efficient, but as programmer reading this, I find the fun...end 
> part
> makes the intent less clear.
>
>
> Similarly:
>
> Fn = fun attr:varName/1,
> lists:map(Fn, rel:attrList(rel:findNamed(ng,"Member"))).
>
> I also find cluttered.
>
> Richard Carlsson suggested the very elegant approach using list
> comprehensions:
>
> [attr:varName(Attr) || Attr <- rel:findNamed(ng,"Member")]
>
> This is what I am using.
>
>
> None-the-less, I am learning Erlang and am interested in the "moderen
> Erlang" way to use map, filter. Perhaps the answer is list 
> comprehensions?
>
>
I would say yes, list comprehension is very elegant for filters (and 
generators).  I don't think I ever used lists:filter - I usally use 
lists:foldl (if map and foreach can't be used), I like foldl because 
it's very flexible and used to be faster than list comprehension 
(R5-6 ?).




More information about the erlang-questions mailing list