Strings and Printf in OCaml
Concatenate with ^, format with %s and %.1f, sprintf returns.
(* ^ concatenates; Printf formats with compile-checked specifiers. *)
let pool = "ledge pool"
let temp_c = 14.5
let () =
print_endline ("Sampling the " ^ pool);
Printf.printf "%s at %.1f C\n" pool temp_c;
(* sprintf returns the string instead of printing. *)
let label = Printf.sprintf "%s-%d" pool 2026 in
print_endline ("Label: " ^ label);
Printf.printf "Uppercase: %s\n" (String.uppercase_ascii pool)
How it works
Printf.printfchecks its format against the arguments at compile time.Printf.sprintfbuilds the string instead of printing it.String.uppercase_asciinames its encoding honestly.
Keywords and builtins used here
inlet
The run, in numbers
- Lines
- 11
- Characters to type
- 404
- Tokens
- 74
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 68 seconds.
Step 3 of 3 in Values, step 3 of 29 in Language basics.