General FizzBuzz: Difference between revisions

Content added Content deleted
Line 2,526: Line 2,526:
A simple Prolog solution to the generalised FizzBuzz problem is as follows:
A simple Prolog solution to the generalised FizzBuzz problem is as follows:


<lang prolog>fizzbuzz(_,[],"").
<lang prolog>go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,Fs,OldRes),
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).


loop(B,B,_).
loop(B,B,_).
Line 2,536: Line 2,533:
Next is A+1, loop(Next,B,Fs).
Next is A+1, loop(Next,B,Fs).


fizzbuzz(_,[],"").
go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).</lang>
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,Fs,OldRes),
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).</lang>

The program can be launched by querying the predicate
<lang prolog>?- go.</lang>


It is worth noting that
It is worth noting that