How to resolve the algorithm Loops/Do-while step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Do-while step by step in the OCaml programming language
Table of Contents
Problem Statement
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Do-while step by step in the OCaml programming language
Source code in the ocaml programming language
let rec loop i =
let i = succ i in
Printf.printf "%d\n" i;
if i mod 6 <> 0 then
loop i
in
loop 0
let do_while f p =
let rec loop() =
f();
if p() then loop()
in
loop()
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)
let v = ref 0 in
do_while (fun () -> incr v; Printf.printf "%d\n" !v)
(fun () -> !v mod 6 <> 0)
let do_while f p ~init =
let rec loop v =
let v = f v in
if p v then loop v
in
loop init
do_while (fun v ->
let v = succ v in
Printf.printf "%d\n" v;
(v))
(fun v -> v mod 6 <> 0)
~init:0
let v = ref 0
exception Exit_loop
try while true do
incr v;
Printf.printf "%d\n" !v;
if not(!v mod 6 <> 0) then
raise Exit_loop;
done
with Exit_loop -> ()
You may also check:How to resolve the algorithm Associative array/Creation step by step in the XLISP programming language
You may also check:How to resolve the algorithm Zhang-Suen thinning algorithm step by step in the Action! programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Haskell programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Go programming language
You may also check:How to resolve the algorithm Semiprime step by step in the F# programming language