English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2008-11-24 (22:37)
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 test.ml
let rec f a b =
  let computation = a + b in
    (computation, f)

This file doesn't compile; the error is:

$ ocamlc -c test.ml
File "test.ml", 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 test.ml
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.