0
votes

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!

1

1 Answers

0
votes

If a function body consists of several expressions separated by ;, the last expression is the value of the function.

I hope this is a good enough hint, as you're already extremely close to what you want.