Version franaise
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
Recursion on
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: Recursion on
Guillaume Yziquel a écrit :
> Hello.
> I've been dabbling with recursive definition of React events.
> Suppose I have a function called schedule of type
>     val schedule : Calendar.t -> unit React.event
> which throws out an event at the specified time or date.
> I've been trying to create a regular_schedule function that throws 
> events at a regular pace. Daniel Bünzli's module is great, but sometimes 
> a bit rough to get by, specifically on examples such as this one.
> So I came up with a recursive definition of such a React.event:
>> let rec regular_schedule start_time period =
>>   React.E.switch React.E.never begin
>>     begin fun () -> regular_schedule (Calendar.add ( ()) 
>> period) period end
>>     begin schedule start_time end
>>   end
> First question:
> It almost works, in the sense that if you insert a print_endline after 
> the fun (), well, it indeeds prints stuff on stdin at the specified 
> pace. However, somehow, the event as a whole behaves as React.E.never. 
> So it doesn't work. I guess it has to do with the way React.switch works 
> synchronously, but I really do not get it.

OK. The following piece of code works out, but it seems to me that using 
a in this position is a rather ugly hack.

let rec regular_schedule start period =
   let waiting_for = schedule start in
   React.E.switch React.E.never begin
     begin fun () -> [waiting_for; regular_schedule
       (Calendar.add ( ()) period) period]
     end waiting_for

> Second question:
> Is there somehow a notion of 'tail recursion' for such constructs?

I gave some thought to it, and it seems quite plausible that what is 
done in the first place is analoguous to tail recursion. And that using breaks the tail recursion. Am I offtrack?

> All the best,

      Guillaume Yziquel