in ocaml, how do i find the max value of a list of integers? if [2; 4] it should give me 4. in javascript

Here's a way to find the max value of a list of integers in OCaml using recursion:

let rec max_of_list lst =
    match lst with
    | [] -> failwith "Empty list"
    | [x] -> x
    | hd::tl -> max hd (max_of_list tl)

let result = max_of_list [2; 4]
167 chars
8 lines

You can run this code in an OCaml environment to find the max value of the list [2; 4], which should give you 4.

related categories

gistlibby LogSnag