Created
May 11, 2020 16:04
-
-
Save slapers/54e0a466255bdfb475214d0245f75899 to your computer and use it in GitHub Desktop.
week1.15 erlang mooc excercise
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(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. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice meta-programming in tests!!