ocaml - Can't make exercise from Jason Hickey's "Introduction to Objective Caml" book -
i'm trying solve exercise:
suppose have function on integers f : int -> int mono- tonically increasing on range of arguments 0 n. is, f < f (i + 1) 0 ≤ < n. in addition f 0 < 0 , f n > 0. write function search f n finds smallest argument f ≥ 0.
now wrote this
let search f n = let min = f 0 in let rec searchin = if >= n min else if f min > f min = searchin i+1;;
but crashes error:
error: parse error: "in" expected after [binding] (in [expr])
what wrong? , implementation correct?
let search f n = let min = f 0 in let rec searchin = if >= n min else if f min > f min = i; searchin i+1 in searchin 0;;
you forgot call function.
anyway false, correct search is
let search f n = let rec searchin = if i>=n failwith("error not possible") else if f >0 i-1 else searchin (i+1) in searchin 0;;
you can search loop
let search f n = let = ref 0 in while f (!i) < 0 i:= !i +1; done; !i;;
Comments
Post a Comment