Last active
December 31, 2018 21:32
-
-
Save christophejunke/f0aa929e5aee03148d5b8b60895e7bf3 to your computer and use it in GitHub Desktop.
FizzBuzz alternative
This file contains 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
% tested with http://eclipseclp.org/ | |
divisors_term(_, [3,5],"FizzBuzz"). | |
divisors_term(_, [3], "Fizz"). | |
divisors_term(_, [5], "Buzz"). | |
divisors_term(N, [], N). | |
% first clause, always fails, but with side-effects | |
fizz_buzz :- | |
% backtrackably iterates N from 1 to 100 by 1 | |
between(1, 100, 1, N), | |
% find all X matching the test into list D | |
findall(X, (member(X, [3,5]), 0 is N mod X), D), | |
% transform list of divisor to term | |
divisors_term(N, D, T), | |
% write term | |
writeln(T), | |
% fail, retry | |
fail. | |
% second clause (trivially succeeds): tried after all choice | |
% points in first clause are exhausted (after iteration). | |
fizz_buzz. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment