A short way to pass zero level lambda functions in OCaml

Is there any short way to pass a function of a null function to another function. So far I am doing it like this:

let fabc = ...;; a (fun () -> fabc) 

Is there any syntactic sugar for the second line?

+6
ocaml
source share
2 answers

What about:

 lazy (fabc) 

To apply apply, use Lazy.force , as in:

 # let l = lazy (2+2) ;; val l : int lazy_t = <lazy> # Lazy.force l ;; - : int = 4 

The value is not quite the same as (fun () -> ...) and ... () , and it is not very shorter. Perhaps if you really need the convenient syntax for lazy or fun () -> , you should use the camlp extension {4,5} for this purpose.

+10
source share

If f were defined as fabc () = ... , you could just do a (fabc) , but other than that, there’s a shorter way.

If you want to define the const function as follows:

 let const x _ = x 

And then use it like this:

 a (const (fabc)) 

But this is actually not much shorter (or clearer) than using fun . It also evaluates fabc immediately, which is probably not the way you want it.

PS: The pedant in me should indicate that (fun () -> ...) is a unary function and there are no zero sequence functions in ocaml.

+6
source share

All Articles