caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Circuralizing lists
@ 2007-11-26 21:46 Till Varoquaux
  2007-11-26 21:58 ` [Caml-list] " Alain Frisch
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Till Varoquaux @ 2007-11-26 21:46 UTC (permalink / raw)
  To: Caml List

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Circuralizing lists
  2007-11-26 21:46 Circuralizing lists Till Varoquaux
@ 2007-11-26 21:58 ` Alain Frisch
  2007-11-26 22:41 ` Jon Harrop
  2007-12-20 21:04 ` David Thomas
  2 siblings, 0 replies; 4+ messages in thread
From: Alain Frisch @ 2007-11-26 21:58 UTC (permalink / raw)
  To: Till Varoquaux; +Cc: Caml List

Till Varoquaux 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?

No, this is not possible (using only safe features). The only recursive 
(non functional) value loops that can be built have a fixed shape.

-- Alain


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Circuralizing lists
  2007-11-26 21:46 Circuralizing lists 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
  2 siblings, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2007-11-26 22:41 UTC (permalink / raw)
  To: caml-list

On Monday 26 November 2007 21:46, Till Varoquaux 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?

OCaml only permits restricted static forms of this construct. The only 
practical application of this restricted form that I am aware of is in 
avoiding dummy values when creating simple cycles such as the environments of 
recursive closures in an interpreter.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Circuralizing lists
  2007-11-26 21:46 Circuralizing lists 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
  2 siblings, 0 replies; 4+ messages in thread
From: David Thomas @ 2007-12-20 21:04 UTC (permalink / raw)
  To: Caml List

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-12-20 21:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-26 21:46 Circuralizing lists 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 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).