From bf63090a54fdef3971570eb4e65f58886986137e Mon Sep 17 00:00:00 2001 From: Auberon Lopez Date: Fri, 29 May 2026 18:41:37 -0700 Subject: [PATCH 1/2] Add Longest Word in OCaml --- archive/o/ocaml/longest-word.ml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 archive/o/ocaml/longest-word.ml diff --git a/archive/o/ocaml/longest-word.ml b/archive/o/ocaml/longest-word.ml new file mode 100644 index 000000000..c3a59f8d6 --- /dev/null +++ b/archive/o/ocaml/longest-word.ml @@ -0,0 +1,20 @@ +(* Use custom function instead of Char.Ascii.is_white because the builtin also matches + on vertical tab and form feed which are not considered whitespace for the + purposes of this problem *) +let is_whitespace = function ' ' | '\t' | '\n' | '\r' -> true | _ -> false + +let longest_word s = + s |> String.to_seq + |> Seq.fold_left + (fun (best, curr) c -> + if is_whitespace c then (best, 0) + else (Int.max best (curr + 1), curr + 1)) + (0, 0) + |> fst + +let () = + print_endline + @@ + match Sys.argv with + | [| _; s |] when s <> "" -> string_of_int (longest_word s) + | _ -> "Usage: please provide a string" From 56a8cb5f7d969ad77ddbde7afa637f019dae4c25 Mon Sep 17 00:00:00 2001 From: Auberon Lopez Date: Fri, 29 May 2026 19:27:47 -0700 Subject: [PATCH 2/2] OCaml longest word: refactor tuple extraction and names for clarity --- archive/o/ocaml/longest-word.ml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/archive/o/ocaml/longest-word.ml b/archive/o/ocaml/longest-word.ml index c3a59f8d6..989983228 100644 --- a/archive/o/ocaml/longest-word.ml +++ b/archive/o/ocaml/longest-word.ml @@ -3,18 +3,21 @@ purposes of this problem *) let is_whitespace = function ' ' | '\t' | '\n' | '\r' -> true | _ -> false -let longest_word s = - s |> String.to_seq - |> Seq.fold_left - (fun (best, curr) c -> - if is_whitespace c then (best, 0) - else (Int.max best (curr + 1), curr + 1)) - (0, 0) - |> fst +let longest_word_len s = + let best, _ = + String.fold_left + (fun (best, curr) c -> + if is_whitespace c then (best, 0) + else + let curr = curr + 1 in + (Int.max best curr, curr)) + (0, 0) s + in + best let () = print_endline @@ match Sys.argv with - | [| _; s |] when s <> "" -> string_of_int (longest_word s) + | [| _; s |] when s <> "" -> string_of_int (longest_word_len s) | _ -> "Usage: please provide a string"