From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id F2C68BC37 for ; Wed, 9 Dec 2009 04:23:32 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnkEAKmmHktV2gB5h2dsb2JhbACEJZRfgl0BAQEKCwgcqxeQN4EvgixXBIkb X-IronPort-AV: E=Sophos;i="4.47,365,1257116400"; d="scan'208";a="38258710" Received: from emailfrontal2.citycable.ch ([85.218.0.121]) by mail2-smtp-roc.national.inria.fr with SMTP; 09 Dec 2009 04:23:32 +0100 Received: from [192.168.0.12] (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal2.citycable.ch (Postfix) with ESMTPA id 144EBB10446; Wed, 9 Dec 2009 04:23:28 +0100 (CET) Message-ID: <4B1F1803.9010203@citycable.ch> Date: Wed, 09 Dec 2009 04:22:43 +0100 From: Guillaume Yziquel Reply-To: guillaume.yziquel@citycable.ch User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: OCaml List , =?UTF-8?B?RGFuaWVsIELDvG56bGk=?= Subject: Re: Recursion on React.events. References: <4B1F0E3A.3040907@citycable.ch> In-Reply-To: <4B1F0E3A.3040907@citycable.ch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; guillaume:01 guillaume:01 recursion:01 recursive:01 val:01 recursive:01 endline:01 stdin:01 recursion:01 rec:01 rec:01 tail:01 tail:01 behaves:02 breaks:02 Guillaume Yziquel a =C3=A9crit : > Hello. >=20 > I've been dabbling with recursive definition of React events. >=20 > Suppose I have a function called schedule of type >=20 > val schedule : Calendar.t -> unit React.event >=20 > which throws out an event at the specified time or date. >=20 > I've been trying to create a regular_schedule function that throws=20 > events at a regular pace. Daniel B=C3=BCnzli's module is great, but som= etimes=20 > a bit rough to get by, specifically on examples such as this one. >=20 > So I came up with a recursive definition of such a React.event: >=20 >> let rec regular_schedule start_time period =3D >> React.E.switch React.E.never begin React.E.map >> begin fun () -> regular_schedule (Calendar.add (Calendar.now ())=20 >> period) period end >> begin schedule start_time end >> end >=20 > First question: >=20 > It almost works, in the sense that if you insert a print_endline after=20 > the fun (), well, it indeeds prints stuff on stdin at the specified=20 > pace. However, somehow, the event as a whole behaves as React.E.never.=20 > So it doesn't work. I guess it has to do with the way React.switch work= s=20 > synchronously, but I really do not get it. OK. The following piece of code works out, but it seems to me that using=20 a React.E.select in this position is a rather ugly hack. let rec regular_schedule start period =3D let waiting_for =3D schedule start in React.E.switch React.E.never begin React.E.map begin fun () -> React.E.select [waiting_for; regular_schedule (Calendar.add (Calendar.now ()) period) period] end waiting_for end > Second question: >=20 > 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=20 done in the first place is analoguous to tail recursion. And that using=20 React.E.select breaks the tail recursion. Am I offtrack? > All the best, --=20 Guillaume Yziquel http://yziquel.homelinux.org/