Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
A function returning itself
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Paolo Donadeo <p.donadeo@g...>
Subject: A function returning itself
Hi everybody, I need your help again.

In a particular application I need (or at least I think so...) to
create a function that, besides it's own computation, returns itself.
Something like:

$ cat
let rec f a b =
  let computation = a + b in
    (computation, f)

This file doesn't compile; the error is:

$ ocamlc -c
File "", line 3, characters 4-20:
This expression has type int * (int -> int -> int * 'a)
but is here used with type int * 'a

If I use the -rectypes option, it seems to work, even though the
inferred type is quite cryptic (to me):

$ ocamlc -c -rectypes -i
val f : int -> int -> int * 'a as 'a

Since I don't completely understand the inner implications of
-rectypes and a quick check in the mailing list archives revealed that
"rectypes is evil", the question I pose is whether is it possible to
avoid the -rectypes option in this particular case, possibly with the
definition of an auxiliary type.

My idea is that the function makes a computation and then decides if
the next computation of the same type will be carried out by "f"
itself, or by another function with identical signature.