caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Grebeniuk <gdsfh1@gmail.com>
To: Jean-Baptiste Jeannin <caml-list@inria.fr>
Subject: Re: [Caml-list] Cyclic data structures: internal representation
Date: Mon, 12 Nov 2012 13:24:30 +0200	[thread overview]
Message-ID: <CAPi0vKU3ETEGJyqJ9-pP8PEzOBa7+r51V9pyyW0uCq5ZBU=0Vw@mail.gmail.com> (raw)
In-Reply-To: <A7FC6DE86AF33142A1FA4CE04EEBF4F18596A9DD62@gobo.cs.cornell.edu>

Hello.

> - is there any easy way to check if a list is cyclic or not? The only way I see is to go down the list, checking at every step if this particular sublist has already been seen. But it's rather heavy.

> - the documentation on the = sign (http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html) mentions that "Equality between cyclic data structures may not terminate." It seems to terminate if one or the other of the data structures is not cyclic. Does it ever terminate when both data sstructures are cyclic, or does it always loop?

  Both these questions are solved with my library ocaml-cyclist:
https://forge.ocamlcore.org/projects/ocaml-cyclist/
  I don't remember exact details, but generally I use
"tortoise and hare" algorithm.

  Also note that lists with a cycle can also contain some prefix
that doesn't appear in the cycle (it happens when list with cycle
is appended to "linear" list).  That's also covered by ocaml-cyclist:

        value length : list 'a -> (int * int);
        (** Returns [(prefix_len, cycle_len)] of the argument.
            (0, 0) for empty list, (n, 0) for linear list,
            (0, n) for circular list, (n, m) for generic-shaped
            cyclic list.  (n, m > 0)
        *)

  As for equality, you can use

        value for_all2 : ?strict:bool ->
          ('a -> 'b -> bool) -> list 'a -> list 'b -> bool;

to write the code like

    let list_eq a b = Cyclist.for_all2 ~strict:true ( = ) a b

which will run correctly.  However, the following lists will be
considered equal: [{1; 2; 3}] and [1; 2; {3; 1; 2; 3; 1; 2}] (curly braces
denote the cycle of list; it's for illustration purposes only).
  Using other library functions you can strenghten your equality
relation.

      parent reply	other threads:[~2012-11-12 11:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-12  6:44 Jean-Baptiste Jeannin
2012-11-12  8:55 ` Basile Starynkevitch
2012-11-12  9:43 ` Gabriel Scherer
2012-11-12 11:24 ` Dmitry Grebeniuk [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='CAPi0vKU3ETEGJyqJ9-pP8PEzOBa7+r51V9pyyW0uCq5ZBU=0Vw@mail.gmail.com' \
    --to=gdsfh1@gmail.com \
    --cc=caml-list@inria.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).