Advertisement
Guest User

Untitled

a guest
Mar 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.96 KB | None | 0 0
  1. -export([any/2, all/2]).
  2.  
  3. -include_lib("eunit/include/eunit.hrl").
  4.  
  5.     %% BEGIN (write your solution here)
  6.     any(_, []) -> false;
  7.     any(Pred, [H|T]) -> Pred(H) orelse Pred([H|T]).
  8.     %% END
  9.  
  10. any_test() ->
  11.     F1 = fun(V) -> V > 10 end,
  12.     ?assertEqual(true, any(F1, [10, 20, 30])),
  13.     ?assertEqual(false, any(F1, [1, 2, 3])),
  14.     ?assertEqual(false, any(F1, [])),
  15.     F2 = fun(V) -> V rem 2 =:= 0 end,
  16.     ?assertEqual(true, any(F2, [1, 2, 3])),
  17.     ?assertEqual(false, any(F2, [1, 3, 5])),
  18.     ok.
  19.  
  20.  
  21.     %% BEGIN (write your solution here)
  22.     all(_, []) -> true;
  23.     all(Pred, [H|T]) -> Pred(H) andalso Pred([H|T]).
  24.     %% END
  25.  
  26. all_test() ->
  27.     F1 = fun(V) -> V >= 10 end,
  28.     ?assertEqual(true, all(F1, [10, 20, 30])),
  29.     ?assertEqual(false, all(F1, [1, 20, 30])),
  30.     ?assertEqual(true, all(F1, [])),
  31.     F2 = fun(V) -> V rem 2 =:= 0 end,
  32.     ?assertEqual(true, all(F2, [4, 6, 8])),
  33.     ?assertEqual(false, all(F2, [2, 3, 4])),
  34.     ok.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement