[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: accumulator generator (O'Caml)



I think the simplest O'Caml function which could be said to
satisfy your specification is something like

  let foo n i = n := !n+i; !n ;;

which is of type  int ref -> (int -> int)
so you could construct an accumulator with an initial value of 23
by using  let acc = foo (ref 23);;


Perhaps more in the spirit of your intention would be a
version which takes an ordinary non-mutable int parameter and
constructs the reference for you:

  let foo n =
    let r = ref n in
      fun i -> r := !r+i; !r;;


 (*robin*)