Skip to content

Instantly share code, notes, and snippets.

@christophejunke
Last active December 31, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophejunke/f0aa929e5aee03148d5b8b60895e7bf3 to your computer and use it in GitHub Desktop.
Save christophejunke/f0aa929e5aee03148d5b8b60895e7bf3 to your computer and use it in GitHub Desktop.
FizzBuzz alternative
% 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