Wednesday, February 12, 2014

Probably My Last Ocaml Post: Finding the Smallest Sublist Size in a List of Lists

let rec minLengthHelper(list, count) = match list with
 |[[]] -> 0
 |[] -> count
 |head::tail -> if List.length head < count then minLengthHelper(tail, List.length head)
                else minLengthHelper(tail, count) in
 minLengthHelper(list, max_int);;

let minLength list = if list == [] then 0 else
 let rec minLengthHelper(list, count) = match list with
 |[] -> count
 |head::tail -> if head < count then minLengthHelper(tail, head)
                else minLengthHelper(tail, count) in
 minLengthHelper(List.map List.length list, max_int);;
A thing to note here, which was tough to find is the max Int size is represented with just max_int.

No comments:

Post a Comment