Create a gist now

Instantly share code, notes, and snippets.

@AndrewBrinker /fizzbuzz.pl Secret
Last active Nov 28, 2017

Embed
% Credit to Rosetta Code [1] with some modifications
%
% [1]: https://rosettacode.org/wiki/FizzBuzz#Prolog
% Unifies V with 'yes' if D divides evenly into N, 'no' otherwise.
divisible_by(N, D, Result) :-
( 0 is N mod D -> Result = yes
; Result = no).
% N, /3?, /5?, Result
fizzbuzz_text(_, yes, yes, 'FizzBuzz').
fizzbuzz_text(_, yes, no, 'Fizz').
fizzbuzz_text(_, no, yes, 'Buzz').
fizzbuzz_text(N, no, no, N).
% Print 'Fizz', 'Buzz', 'FizzBuzz' or N as appropriate.
fizzbuzz(Number) :-
divisible_by(Number, 3, IsFizz),
divisible_by(Number, 5, IsBuzz),
fizzbuzz_text(N, IsFizz, IsBuzz, Result),
format("~p -> ~p~n", [Number, Result]).
main :-
foreach(between(1, 100, N), fizzbuzz(N)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment