OCaml with list instead of array
let rec closest num list = match list with
 -> failwith "Empty list"
|[n] -> n
|h :: q ->
let closest_q = closest num q in
let val_h = abs (num - h) in
let val_q = abs (num - closest_q) in
if (val_h < val_q) then h else closest_q;;
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.