help a newbie with Lazy
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Hi. I am trying to teach myself OCaml, so I was starting with the examples from John Hughes' paper on functional design. In particular I was trying to get his algorithm for square root to work - let next y x = (x +. y /. x) /. 2.;; let rec repeat f x = x::repeat f (f x);; let rec within eps (a::b::rest) = if abs_float(a -. b)<eps then b else within eps (b::rest);; let newton_sqrt x0 eps y = within eps (repeat (next y) x0);; newton_sqrt 4. 1e-5 17.;; This fails, because I guess OCaml is not lazy by default. I suppose I need to apply a 'lazy' and a 'Lazy.force' in here somewhere, but I'm not sure where. I made various attempts without success. Can anyone give me a hint? Regards, John