How to resolve the algorithm FizzBuzz step by step in the Mercury programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm FizzBuzz step by step in the Mercury programming language
Table of Contents
Problem Statement
Write a program that prints the integers from 1 to 100 (inclusive).
But:
The FizzBuzz problem was presented as the lowest level of comprehension required to illustrate adequacy.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the Mercury programming language
Source code in the mercury programming language
:- module fizzbuzz.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, string, bool.
:- func fizz(int) = bool.
fizz(N) = ( if N mod 3 = 0 then yes else no ).
:- func buzz(int) = bool.
buzz(N) = ( if N mod 5 = 0 then yes else no ).
% N 3? 5?
:- func fizzbuzz(int, bool, bool) = string.
fizzbuzz(_, yes, yes) = "FizzBuzz".
fizzbuzz(_, yes, no) = "Fizz".
fizzbuzz(_, no, yes) = "Buzz".
fizzbuzz(N, no, no) = from_int(N).
main(!IO) :- main(1, 100, !IO).
:- pred main(int::in, int::in, io::di, io::uo) is det.
main(N, To, !IO) :-
io.write_string(fizzbuzz(N, fizz(N), buzz(N)), !IO),
io.nl(!IO),
( if N < To then
main(N + 1, To, !IO)
else
true
).
You may also check:How to resolve the algorithm Greatest common divisor step by step in the m4 programming language
You may also check:How to resolve the algorithm Binary search step by step in the Picat programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the Lua programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Ada programming language