English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Circuralizing lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-12-20 (21:04)
From: David Thomas <david_hd@y...>
Subject: Re: [Caml-list] Circuralizing lists
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:
> 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