Multiplication tables: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: using the * instead of sprintf in formats (reformatting as consequence of editing with emacs...))
(added ocaml)
Line 276: Line 276:
│12│ 144│
│12│ 144│
└──┴────────────────────────────────────────────────┘</lang>
└──┴────────────────────────────────────────────────┘</lang>

=={{header|OCaml}}==
{{trans|C}}

<lang ocaml>let format_of_string (s :string) = (Obj.magic s : ('a, 'b, 'c) format)

let () =
let max = 12 in
let fmax = float_of_int max in

let dgts = int_of_float (0.99 +. log10(1.0 *. fmax *. fmax)) in
let fmt = format_of_string(Printf.sprintf " %%%dd" dgts) in
let fmt2 = format_of_string(Printf.sprintf "%%%ds%%c" dgts) in

Printf.printf fmt2 "" 'x';
for i = 1 to max do Printf.printf fmt i done;
print_string "\n\n";

for j = 1 to max do
Printf.printf fmt j;
for i = 1 to pred j do Printf.printf fmt2 "" ' '; done;
for i = j to max do Printf.printf fmt (i*j); done;
print_newline()
done;
print_newline()</lang>

the function ''format_of_string'' can also be written like this:

<lang ocaml>external format_of_string : string -> ('a, 'b, 'c) format = "%identity"</lang>



=={{header|Perl}}==
=={{header|Perl}}==