Last active
May 7, 2020 07:13
-
-
Save adolfont/61b150091e71c972fb9eb4c9be4011ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done!