Skip to content

Instantly share code, notes, and snippets.

@adolfont
Last active May 7, 2020 07:13
Show Gist options
  • Select an option

  • Save adolfont/61b150091e71c972fb9eb4c9be4011ac to your computer and use it in GitHub Desktop.

Select an option

Save adolfont/61b150091e71c972fb9eb4c9be4011ac to your computer and use it in GitHub Desktop.
-module(logic).
% In the previous video step on pattern matching we saw two ways of defining
% “exclusive or”. Give at least three others. You might find it useful to
% know that:
% =/= and == are the operations for inequality and equality in Erlang;
% not is the Erlang negation function; and,
% and and or are the Erlang conjunction and disjunction (infix) operators.
-export([exclusive_or_1/2,exclusive_or_2/2,exclusive_or_3/2,exclusive_or_4/2,
exclusive_or_5/2, max_three/3, how_many_equal/3, test_exclusive_or_1/0]).
exclusive_or_1(true,false) ->
true;
exclusive_or_1(false,true) ->
true;
exclusive_or_1(_,_) ->
false.
exclusive_or_2(X,X) ->
false;
exclusive_or_2(_,_) ->
true.
exclusive_or_3(Left, Right) ->
Left =/= Right and
((Left==true) or (Left==false))and
((Right==true) or (Right==false)).
exclusive_or_4(Left, Right) ->
(Left and not Right) or (not Left and Right).
exclusive_or_5(Left, Right) ->
not ((Left and Right) or (not Left and not Right)).
max_three(First, Second, Third) ->
max(First, max(Second, Third)).
how_many_equal(X,X,X) -> 3;
how_many_equal(X,X,_) -> 2;
how_many_equal(X,_,X) -> 2;
how_many_equal(_,X,X) -> 2;
how_many_equal(_,_,_) -> 0.
test_exclusive_or_1() ->
true = exclusive_or_1(true, false),
true = exclusive_or_1(false, true),
false = exclusive_or_1(true, true),
false = exclusive_or_1(false, false),
all_good.
@elbrujohalcon

Copy link
Copy Markdown

Nicely done!

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