Coming to OCaml from Lisp, I find myself very confused about when functions return and when they don’t. I miss my magic Quote! Thankfully, most of the time, OCaml appears to automagicly know when I want a function evaluated and when I don’t. However, I frequently find myself trying to assign the return value of a function in a let expression, like the following.
let start = Sys.time in
(*
* do something here
*)
;
let ending = Sys.time in
Printf.printf "did something in %f seconds\n" (ending -. start)
but then ocamlc complains
Error: This Expression has type unit -> float
but an expression was expected of type float
Telling me that start and end are bound to Sys.time, not the return value of Sys.time.
Is this behavior I’m trying to get not OCamly? Do I want to be doing things another way? Am I just missing something completely obvious?
A function is evaluated when you apply it to an argument. I.e. when you do
f,fnever gets evaluated. When you dof x,falways gets evaluated. There’s nothing magical about it.As you correctly pointed out,
Sys.timeis a function (of typeunit -> float) andlet start = Sys.timejust assigns that function tostart.To get the behavior you want simply do
let start = Sys.time (), which applies the functionSys.timeto the argument()(which is the only value of typeunit).