Companion code for paper "Imperative Functional Programs that Explain their Work", Wilmer Ricciotti, Jan Stolarek, Roly Perera and James Cheney, ICFP 2017, Oxford, UK
let poly = fun poly (x : double) : double => x * x * x + 1.0 in
let poly' = fun poly' (x : double) : double => 3.0 * x * x in
let x = ref 0.0 in
-- Newton's method.
-- f : function which root want to find
-- f' : deribative of f
-- n : number of iterations to perform
let newton = fun newton (f : double -> double) (f' : double -> double)
(n : int) : double =>
if n == 0
then !x
else x := !x - (f( !x ) / f'( !x )) ;;
newton f f' (n - 1) in
When I load this file into a REPL:
I get