28 ms·
Just for fun, here's the Modula-2 example converted into OCaml in a few minutes of effort: let size = 8191 let flags = Array.make size false let pr
by yawaramin 4y ago
Just for fun, here's the Modula-2 example converted into OCaml in a few minutes of effort:
let size = 8191
let flags = Array.make size false
let prime = ref 0
let k = ref 0
let count = ref 0
let () =
print_endline "Type Return";
ignore (input_char stdin);
print_endline "10 iterations";
for iter = 1 to 10 do
count := 0;
for i = 0 to size - 1 do flags.(i) <- true done;
for i = 0 to size - 1 do
if flags.(i) then
prime := i + i + 3;
k := i + !prime;
while !k < size do
flags.(!k) <- false;
k := !k + !prime;
done;
incr count;
done;
done;
Printf.printf "%d primes\n%!" !count
If one didn't look closely it could easily be mistaken for Pascal/Modula-2. OCaml lifts a lot of imperative constructs directly from that lineage while adding its own FP flavour i.e. immutability by default meaning that mutable values must be ref or array values.
Another aspect that OCaml lifts pretty directly from Modula-2 is the engineering of its separate compilation via its module system. By comparing the compiled interface files, a build system like dune or ReScript can figure out the least amount of recompilation needed to build a project from a dirty state.