For fun, I did the same for OCaml:
Ex 1
let say = "I love OCaml"
let () = print_endline say
(* Requires linking in the 'str' library *)
let say = Str.replace_first (Str.regexp {|\(.*\)love\(.*\)|}) {|\1*love*\2|} say;;
let () = print_endline (String.uppercase_ascii say)
let () = ignore |> Seq.init 5 |> Seq.iter (fun () -> print_endline say)
Ex 2 module StringSet = Set.Make(String)
let cities = StringSet.of_list [
"London";
"Oslo";
"Paris";
"Amsterdam";
"Berlin";
]
let visited = StringSet.of_list ["Berlin"; "Oslo"]
(* Requires the 'fmt' library *)
let string_set fmt v = Fmt.Dump.list Fmt.string fmt (StringSet.to_list v)
let () =
Format.printf "I still need to visit the following cities: %a\n"
string_set
(StringSet.diff cities visited)
Ex 3 module Greeter : sig
type t
val make : string -> t
val salute : t -> unit
end = struct
type t = { name : string }
let make name = { name = String.uppercase_ascii name }
let salute t = Format.printf "Hello %s\n" t.name
end
let g = Greeter.make "world"
let () = Greeter.salute g
Obviously, OCaml is a much lower-level language than Ruby or Raku–notably, regex support is not as great, and we have to explicitly tell it how to print values of custom types. Still, I find its lack of syntax sugar makes it easy to read nearly any OCaml code I come across in the wild!