map and filter with list comprehensions
James Hague
jamesh@REDACTED
Thu Sep 19 21:26:50 CEST 2002
Map and filter can be written with list comprehensions as follows:
map(Fun, L) ->
[Fun(X) || X <- L].
filter(Pred, L) ->
[X || X <- L, Pred(X)].
In the lists module of the standard library, filter is written using list
comprehensions, just as above. Map looks like this:
map(F, [H|T]) ->
[F(H)|map(F, T)];
map(F, []) ->
[].
Is there a reason why the list comprehension version isn't preferred?
More information about the erlang-questions
mailing list