Programming Challenges
Explore practical programming challenges in OCaml that involve working with compound data types such as strings, lists, binary trees, and natural numbers. Learn to write functions to find the longest string, concatenate strings, compute tree height, and perform natural number operations. This lesson strengthens your functional programming skills by applying key concepts to real coding problems.
Challenge 1: Find the longest string in a string list
Write an OCaml function, longest_string: string list -> string option, to find the longest string in a list of strings.
Examples:
longest_string [] = None
longest_string ["a"; "ABC"; "ab"] = Some "abc"
Hint: You can use OCaml’s String.length function to compute the length of a string.
Challenge 2: Concatenate strings
Write an OCaml function, concat: string -> string list -> string, to add a string s and a list of string ...