Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.90 KB | None | 0 0
  1. -module(assignment1).
  2. % Uncompile following line to don't compile the tests
  3. %-define(NOTEST, false).
  4. % Run eunit:test(assignment1). to run the tests
  5. -include_lib("eunit/include/eunit.hrl").
  6. -export([doubleAll/1, evens/1, product/1, zip/2, zip_with/3]).
  7.  
  8. % Define the functions doubleAll, evens, and product using
  9. % the higher-order functions:
  10. % lists:map, lists:filter and lists:foldr
  11.  
  12. doubleAll(XList) -> lists:map(fun(X) -> 2*X end, XList).
  13.  
  14. doubleAll_test_() ->
  15. [
  16.     ?_assert(doubleAll([]) == []),
  17.     ?_assert(doubleAll([1, 2, 3]) =:= [2, 4, 6])
  18. ].
  19.  
  20.  
  21. evens(XList) -> lists:filter(fun(X) -> X rem 2 == 0 end, XList).
  22.  
  23. evens_test_() ->
  24. [
  25.     ?_assert(evens([]) == []),
  26.     ?_assert(evens([1, 2, 3]) =:= [2]),
  27.     ?_assert(evens([1, 2, 3, 4, 5, 6]) =:= [2, 4, 6])
  28. ].
  29.  
  30.  
  31. product(XList) -> lists:foldr(fun(X, Y) -> X*Y end, 1, XList).
  32.  
  33. product_test_() ->
  34. [
  35.     ?_assert(product([1, 2, 3]) == 6)
  36. ].
  37.  
  38. % a) Define a function zip/2 that “zips together” pairs of elements
  39. % from two lists like this:
  40. % zip([1,3,5,7], [2,4]) = [ {1,2}, {3,4} ]
  41. % where you can see that the elements from the longer list are lost.
  42.  
  43. % b) Define a function zip_with/3 that “zips together”
  44. % pairs of elements from two lists using the function in the first argument,
  45. % like this:
  46. % zip_with(fun(X,Y) -> X+Y end, [1,3,5,7], [2,4]) = [ 3, 7 ]
  47.  
  48. % c) Re-define the function zip_with/3 using zip and lists:map.
  49. % d) Re-define zip/2 using zip_with/3.
  50.  
  51. % zip function (a)
  52. zip(X, Y) -> zip(X, Y, []).
  53. zip([X|Xs], [Y|Ys], Acc) -> [{X, Y}] ++ zip(Xs, Ys, Acc);
  54. zip([], _, Acc) -> Acc;
  55. zip(_, [], Acc) -> Acc.
  56.  
  57. zip_test_() ->
  58. [
  59.     ?_assert(zip([1,2,3], [4,5,6]) =:= [{1,4}, {2,5}, {3,6}]),
  60.     ?_assert(zip([1,2,3], [4,5]) =:= [{1,4}, {2,5}]),
  61.     ?_assert(zip([1,2], [4,5,6]) =:= [{1,4}, {2,5}])
  62. ].
  63.  
  64. % zip_with function (b)
  65. zip_with(Fun, X, Y) -> zip_with(Fun, X, Y, []).
  66. zip_with(Fun, [X|Xs], [Y|Ys], Acc) -> [Fun(X, Y)] ++ zip_with(Fun, Xs, Ys, Acc);
  67. zip_with(_, [], _, Acc) -> Acc;
  68. zip_with(_, _, [], Acc) -> Acc.
  69.  
  70. zip_with_test_() ->
  71. [
  72.     ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2,3], [4,5,6]) =:= [4, 10, 18]),
  73.     ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2,3], [4,5]) =:= [4, 10]),
  74.     ?_assert(zip_with(fun(X, Y) -> X*Y end, [1,2], [4,5,6]) =:= [4, 10])
  75. ].
  76.  
  77. % zip_with2 function (c)
  78. zip_with2(Fun, X, Y) -> lists:map(fun({XI,YI}) -> Fun(XI, YI) end, zip(X, Y)).
  79.  
  80. zip_with2_test_() ->
  81. [
  82.     ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2,3], [4,5,6]) =:= [4, 10, 18]),
  83.     ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2,3], [4,5]) =:= [4, 10]),
  84.     ?_assert(zip_with2(fun(X, Y) -> X*Y end, [1,2], [4,5,6]) =:= [4, 10])
  85. ].
  86.  
  87. % zip3 function (d)
  88. zip3(X, Y) -> zip_with2(fun(XI, YI) -> {XI,YI} end, X, Y).
  89.  
  90. zip3_test_() ->
  91. [
  92.     ?_assert(zip3([1,2,3], [4,5,6]) =:= [{1,4}, {2,5}, {3,6}]),
  93.     ?_assert(zip3([1,2,3], [4,5]) =:= [{1,4}, {2,5}]),
  94.     ?_assert(zip3([1,2], [4,5,6]) =:= [{1,4}, {2,5}])
  95. ].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement