Skip to content

Instantly share code, notes, and snippets.

@adhalanay
Created May 27, 2020 09:29
Show Gist options
  • Save adhalanay/75263abc3dc8c3ed388045ac3defa697 to your computer and use it in GitHub Desktop.
Save adhalanay/75263abc3dc8c3ed388045ac3defa697 to your computer and use it in GitHub Desktop.
-module(hofs).
-export[double_All/1,evens/1,prod/1,zip/2,zip_with/3,zip_with_hof/3,zip_hof/2].
double(X)->
2*X.
double_All(Lst) ->
lists:map(fun double/1, Lst).
even(X) ->
X rem 2 == 0.
evens(Lst) ->
lists:filter(fun even/1, Lst).
prod(X,Y) ->
X*Y.
prod(Lst)->
lists:foldr(fun prod/2,1,Lst).
zip(Xs,Ys)->
zip(Xs,Ys,[]).
zip([],_,Acc) ->
Acc;
zip(_,[],Acc) ->
Acc;
zip([X|Xs],[Y|Ys],Acc) ->
[{X,Y}|zip(Xs,Ys,Acc)].
zip_with(F,Xs,Ys)->
zip_with(F,Xs,Ys,[]).
zip_with(_F,[],_,Acc)->
Acc;
zip_with(_F,_,[],Acc) ->
Acc;
zip_with(F,[X|Xs],[Y|Ys],Acc) ->
[F(X,Y)|zip_with(F,Xs,Ys,Acc)].
zip_with_hof(F,Xs,Ys)->
lists:map(fun({X,Y})->F(X,Y) end ,zip(Xs,Ys)).
zip_hof(Xs,Ys) ->
zip_with_hof(fun(X,Y)-> {X,Y} end , Xs,Ys).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment