-module(folds).
-export([max/1, max_fold/1, double_each/1]).

max([H|T]) -> max_func(T, H).

max_fold([H|T]) ->
  Func = fun
    (A, B) when A > B -> A;
    (_, B) -> B
  end,
  fold(Func, H, T).

double_each(List) ->
  Func = multiply(2),
  lists:reverse(map(Func, [], List)).

multiply(Times) -> fun (Num) -> Num * Times end.

max_func([], Max) -> Max;
max_func([H|T], Max) when H > Max -> max_func(T, H);
max_func([_H|T], Max) -> max_func(T, Max).

fold(_, Result, []) -> Result;
fold(F, Result, [H|T]) -> fold(F, F(H, Result), T).

map(_, Result, []) -> Result;
map(F, Result_List, [H|T]) -> map(F, [F(H)|Result_List], T).

Изменить пасту