Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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);
  res

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


Cheers,

-- 
Stéphane