For one of my assignments, I have to write a function in OCaml that takes in an integer argument and returns the previous integer argument. When the function is first called, the function simply returns a zero.
The type has to be:
val prev : int -> int = <fun>
This is what I have so far:
let prev =
let x = ref 0 in
fun n ->
(Printf.printf "prev value: %i." !x);
(x := n)
;;
I can't figure out how to simply return the previous value rather than printing it. I printed the value to make sure it gets updated, but I don't understand how to make the return type "int".
Any help would be appreciated! Thank you!