Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(assignment1).
- % Uncompile following line to don't compile the tests
- %-define(NOTEST, false).
- % Run eunit:test(assignment1). to run the tests
- -include_lib("eunit/include/eunit.hrl").
- -export([doubleAll/1, evens/1, product/1, zip/2, zip_with/3]).
- % Define the functions doubleAll, evens, and product using
- % the higher-order functions:
- % lists:map, lists:filter and lists:foldr
- doubleAll(XList) -> lists:map(fun(X) -> 2*X end, XList).
- doubleAll_test_() ->
- [
- ?_assert(doubleAll([]) == []),
- ?_assert(doubleAll([1, 2, 3]) =:= [2, 4, 6])
- ].
- evens(XList) -> lists:filter(fun(X) -> X rem 2 == 0 end, XList).
- evens_test_() ->
- [
- ?_assert(evens([]) == []),
- ?_assert(evens([1, 2, 3]) =:= [2]),
- ?_assert(evens([1, 2, 3, 4, 5, 6]) =:= [2, 4, 6])
- ].
- product(XList) -> lists:foldr(fun(X, Y) -> X*Y end, 1, XList).
- product_test_() ->
- [
- ?_assert(product([1, 2, 3]) == 6)
- ].
- % a) Define a function zip/2 that “zips together” pairs of elements
- % from two lists like this:
- % zip([1,3,5,7], [2,4]) = [ {1,2}, {3,4} ]
- % where you can see that the elements from the longer list are lost.
- % b) Define a function zip_with/3 that “zips together”
- % pairs of elements from two lists using the function in the first argument,
- % like this:
- % zip_with(fun(X,Y) -> X+Y end, [1,3,5,7], [2,4]) = [ 3, 7 ]
- % c) Re-define the function zip_with/3 using zip and lists:map.
- % d) Re-define zip/2 using zip_with/3.
- % zip function (a)
- zip(X, Y) -> zip(X, Y, []).
- zip([X|Xs], [Y|Ys], Acc) -> [{X, Y}] ++ zip(Xs, Ys, Acc);
- zip([], _, Acc) -> Acc;
- zip(_, [], Acc) -> Acc.
- zip_test_() ->
- [
- ?_assert(zip([1,2,3], [4,5,6]) =:= [{1,4}, {2,5}, {3,6}]),
- ?_assert(zip([1,2,3], [4,5]) =:= [{1,4}, {2,5}]),
- ?_assert(zip([1,2], [4,5,6]) =:= [{1,4}, {2,5}])
- ].
- % zip_with function (b)
- zip_with(Fun, X, Y) -> zip_with(Fun, X, Y, []).
- zip_with(Fun, [X|Xs], [Y|Ys], Acc) -> [Fun(X, Y)] ++ zip_with(Fun, Xs, Ys, Acc);
- zip_with(_, [], _, Acc) -> Acc;
- zip_with(_, _, [], Acc) -> Acc.
- zip_with_test_() ->
- [
- ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2,3], [4,5,6]) =:= [4, 10, 18]),
- ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2,3], [4,5]) =:= [4, 10]),
- ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2], [4,5,6]) =:= [4, 10])
- ].
- % zip_with2 function (c)
- zip_with2(Fun, X, Y) -> lists:map(fun({XI,YI}) -> Fun(XI, YI) end, zip(X, Y)).
- zip_with2_test_() ->
- [
- ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2,3], [4,5,6]) =:= [4, 10, 18]),
- ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2,3], [4,5]) =:= [4, 10]),
- ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2], [4,5,6]) =:= [4, 10])
- ].
- % zip3 function (d)
- zip3(X, Y) -> zip_with2(fun(XI, YI) -> {XI,YI} end, X, Y).
- zip3_test_() ->
- [
- ?_assert(zip3([1,2,3], [4,5,6]) =:= [{1,4}, {2,5}, {3,6}]),
- ?_assert(zip3([1,2,3], [4,5]) =:= [{1,4}, {2,5}]),
- ?_assert(zip3([1,2], [4,5,6]) =:= [{1,4}, {2,5}])
- ].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement