Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: David Thomas <david_hd@yahoo.com>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Circuralizing lists
Date: Thu, 20 Dec 2007 13:04:13 -0800 (PST)	[thread overview]
Message-ID: <730161.46080.qm@web30504.mail.mud.yahoo.com> (raw)
In-Reply-To: <9d3ec8300711261346r4b74f5gddce229324dcad2c@mail.gmail.com>

This depends what you mean by "at all possible."  With
the built in lists, the answers others have given are
correct - the short answer being "no."  That said, if
it's exceedingly useful for your application, you
could do something like:

type 'a rlist = Empty | Cons of 'a * 'a rlist ref;;

let circularize l =
        let l1 = ref Empty in
        let l2 = List.fold_right (fun a b -> ref (Cons
(a, b))) l l1 in
        l1 := !l2; l1;;

Most of what is in List is pretty trivial to duplicate
for the above type, and you could hide the mutability.
 Of course, matching is no longer pretty... but "at
all possible"?  Yes.

--- Till Varoquaux <till.varoquaux@gmail.com> wrote:

> Writing the list containing an inifinite number of
> ones can easily be done as:
> 
> let rec ones = 1::ones
> 
> I however don't know of any type safe to generate
> the infinite list which is the repetition of a given

> list (in a type safe non lazy way).
> What I'm looking for is a code that would do:
> 
> let circularize  = function
>     | [] -> failwith "cannot circularize empty
> lists"
>     | l -> let rec res = l@res in res
> 
> Is this at all possible?
> 
> Cheers,
> Till
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
>
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 



      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping


      parent reply	other threads:[~2007-12-20 21:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-26 21:46 Till Varoquaux
2007-11-26 21:58 ` [Caml-list] " Alain Frisch
2007-11-26 22:41 ` Jon Harrop
2007-12-20 21:04 ` David Thomas [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=730161.46080.qm@web30504.mail.mud.yahoo.com \
    --to=david_hd@yahoo.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