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
forbidden construct as right hand side of "let rec"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-10-25 (15:04)
From: Stéphane Glondu <steph@g...>
Subject: Re: [Caml-list] forbidden construct as right hand side of "let rec"
Mathias Kende a écrit :
> [...] I also want to write :
> 	let rec h = t (f h)
> (with t : ('a -> 'b) -> 'a -> 'b) but here, I can't afford to use
> 	let rec h x = t (f h) x
> because t as some side effects and I need it to be evaluated only once.

Then what about:

let h =
  let tmp = ref (fun x -> assert false) in
  let res x = !tmp x in
  tmp :=
    (fun x ->
       let y = t (f res) in
       tmp := y;
       y x);

Intuitively, the "tmp" reference caches the call to "t (f h)", but this
is otherwise the same technique as I gave earlier.