[erlang-questions] Can I do the same with a fold easily

ok <>
Mon Oct 19 04:44:10 CEST 2015


Roelof Wobben had

> filter(List, Filter) ->
>     lists:filter(Filter, List).

<name list thanks to HTML mail> asked:

> What is the point of this?

Roelof Wobben replied


> These were my solutions to these exercises
>       Add a higher-order function to math_functions.erl
> called filter(F, L) which returns all the elements X in L
> for which F(X) is true.

But you did not do that.  And you did two things wrong.
First, you swapped the argument order to L, F instead of
the F, L order that you were asked for.
Second, you re-used someone *else's* filter/2 function
instead of writing your own, as the exercise asked.
In software engineering, such reuse is a good thing; the
whole Erlang/OTP system *exists* to be reused like that.
But for an exercise, you were supposed to write

filter(F, [X|Xs]) ->
    ????,
    case F(X)
      of true  -> ????
       ; false -> ????
    end,
    ????;
filter(_, []) ->
    ????.

or something like that, filling in ???? appropriately.






More information about the erlang-questions mailing list