caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Jean-Denis Eiden <jean-denis.eiden@orange.fr>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] listes qui bouclent
Date: Sun, 7 Sep 2014 14:24:10 +0100	[thread overview]
Message-ID: <CAAxsn=E7ATU_pjC=i_S8PCas4N1rN6FVyHLv06Fx23dPc7f+-g@mail.gmail.com> (raw)
In-Reply-To: <91F16B653D20446DA42F7B673FD5509D@PCdejd>

[This message is intentionally but regrettably written in English.]

Dear Jean-Denis,

Caml Light is not really maintained any more, so unless you have a
special reason to use it it might be better to switch to OCaml.  I'll
suggest an answer to your question for OCaml, which might not apply
directly to Caml Light.

2014-09-07 10:31 GMT+01:00 Jean-Denis Eiden <jean-denis.eiden@orange.fr>:
> Cela étant, j'aimerais pouvoir faire la même chose en paramétrant la période
> ; par exemple, j'aimerais créer [ 0 ; 1 ; 2 ; ... ; n-1 ; 0 ; 1 ; 2 ; ... ]

I don't know of a way to create cyclic lists of a runtime specified
period using the built-in list type, and I suspect that it isn't
possible.  However, if you're willing to use an alternative list type
then it is possible to accomplish what you want.

The natural setting for a cyclic immutable value is a lazy language.
OCaml is eagerly evaluated by default, but provides support for
introducing laziness explicitly.  We can define a type of lazy lists
in OCaml as follows:

   type 'a llist_aux = LNil | LCons of 'a * 'a llist
   and 'a llist = 'a llist_aux Lazy.t

Using this definition we can write a function which creates cyclic
values of a given period:

   let lcyclic period =
     if period <= 0 then invalid_arg "lcyclic";
     let rec loop i =
       if i = period then l
       else lazy (LCons (i, loop (i + 1)))
     and l = lazy (Lazy.force (loop 0)) in
     l

If you trace through the operation of lcyclic you'll see that a call like this

   lcyclic 3

builds a value that's equivalent to the value created by writing this

   let rec l3 =
      lazy (LCons (0,
      lazy (LCons (1,
      lazy (LCons (2, l3))))))))

Since OCaml has special support for lazy values there's less overhead
than you might imagine in programming with values like this.  Once
every element of the list has been examined the runtime system can
make the representation exactly the same as a corresponding value of
the built-in list type.  However, there's still a little overhead
involved in examining values of llist.

A second approach to building cyclic lists of runtime-specified period
is to use mutation.  If you make the tail of your list values mutable
then you can build the list up to the cyclic point, then patch things
up so that the tail of the last cons cell points back to the front of
the list.  Here's a type definition:

   type 'a mlist = MNil | MCons of 'a mcons
   and 'a mcons = { mhead : 'a; mutable mtail : 'a mlist }

and here's a corresponding function for building cyclic lists:

   let mcyclic period =
     if period <= 0 then invalid_arg "mcyclic";
     let rec mlast = { mhead = period - 1; mtail = MCons mlast }
     and loop i head =
       if i = 0 then head
       else loop (i - 1) (MCons { mhead = i - 1; mtail = head }) in
     let l = loop (period - 1) (MCons mlast) in
        mlast.mtail <- l;
        l

Once again, if you trace through the evaluation of an expression like this

   mcyclic 3

you'll see that it builds a value equivalent to the value created by
writing this

   let rec m3 =
    MCons {mhead = 0; mtail =
    MCons {mhead = 1; mtail =
    MCons {mhead = 2; mtail = m3}}}

Using mutation here introduces a little extra representation overhead
in OCaml, since mutation requires a record, which involves allocating
an extra block for every cons cell.  There's consequently also an
extra level of indirection when examining values of mlist compared to
examining values of the built-in list type.

I hope that helps a bit,

Jeremy.

      parent reply	other threads:[~2014-09-07 13:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-07  9:31 Jean-Denis Eiden
2014-09-07  9:55 ` Rémy Besognet
2014-09-07 13:24 ` Jeremy Yallop [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=E7ATU_pjC=i_S8PCas4N1rN6FVyHLv06Fx23dPc7f+-g@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=jean-denis.eiden@orange.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).