Skip to content

Instantly share code, notes, and snippets.

@slapers
Created May 11, 2020 16:04
Show Gist options
  • Select an option

  • Save slapers/54e0a466255bdfb475214d0245f75899 to your computer and use it in GitHub Desktop.

Select an option

Save slapers/54e0a466255bdfb475214d0245f75899 to your computer and use it in GitHub Desktop.
week1.15 erlang mooc excercise
-module(mooc).
-export([xOr1/2, xOr2/2, xOr3/2]).
-export([maxThree/3]).
-export([howManyEqual/3]).
-export([test/0]).
xOr1(X, Y) ->
(not X == Y) and (X or Y).
xOr2(X, Y) ->
(X /= Y) and (X or Y).
xOr3(X, Y) when X /= Y ->
X or Y;
xOr3(_, _) ->
false.
maxThree(X, Y, Z) ->
max(max(X, Y), Z).
howManyEqual(X, X, X) ->
3;
howManyEqual(X, X, _) ->
2;
howManyEqual(_, X, X) ->
2;
howManyEqual(X, _, X) ->
2;
howManyEqual(_, _, _) ->
0.
check(_, []) ->
true;
check(TruthTable, [H | T]) ->
check(TruthTable, H) and check(TruthTable, T);
check([], _Fun) ->
true;
check([{Args, Expected} | T], Fun) ->
(Expected == apply(?MODULE, Fun, Args)) and check(T, Fun).
test() ->
true = check([{[true, true], false},
{[true, false], true},
{[false, true], true},
{[false, false], false}],
[xOr1, xOr2, xOr3]),
true = check([{[1, 2, 3], 3}, {[0, 0, 0], 0}, {[3, 2, 1], 3}], maxThree),
true = check([{[0, 1, 2], 0},
{[0, 0, 1], 2},
{[0, 1, 0], 2},
{[1, 0, 0], 2},
{[0, 0, 0], 3}],
howManyEqual),
ok.
@elbrujohalcon
Copy link

Nice meta-programming in tests!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment