is there a ruby's lst.max in ocaml in ruby

In Ruby, you can use the max method on an array to find the maximum element in the array. Here is an example:

main.rb
lst = [3, 7, 1, 10, 5]
puts lst.max
36 chars
3 lines

In OCaml, you can achieve a similar result using the List.fold_left function to iterate over the list and keep track of the maximum element. Here is an example:

let find_max lst =
    List.fold_left (fun acc x -> if x > acc then x else acc) (List.hd lst) lst

let lst = [3; 7; 1; 10; 5]
let max_val = find_max lst
print_int max_val
171 chars
7 lines

This OCaml code defines a function find_max which uses List.fold_left to find the maximum element in a list.

related categories

gistlibby LogSnag