From: Peter Frey <pjfrey@sympatico.ca>
To: caml-list@inria.fr
Cc: Eric Jaeger <eric.jaeger@ssi.gouv.fr>
Subject: Re: [Caml-list] Function returning recursive lists
Date: Fri, 21 Dec 2012 14:55:26 -0500 [thread overview]
Message-ID: <BLU0-SMTP100167F680F50824B119393A3360@phx.gbl> (raw)
In-Reply-To: <003801cddcfa$f66325c0$e3297140$@jaeger@ssi.gouv.fr>
On Tue, 2012-12-18 at 09:37 +0100, Eric Jaeger wrote:
> Hi everyone,
>
>
>
> There are various discussions on recursive lists in the archive, yet I
> was wondering whether or not it was possible in pure OCaml to write a
> function returning non-constant recursive lists.
>
>
>
> For example, I would like to have a function “docycle:’a list->’a
> list” that takes a non recursive list and transforms it into a
> recursive list containing the same elements. That is, “docycle
> [1;2;3]” would return a list structurally equivalent to “let rec
> c=1::2::3::c in c”. So far, my various attempts (OCaml 3.12) have not
> been successful. Another good example is to have a List.map compatible
> with recursive lists.
>
>
>
> Please note that it is, in a way, a theoretical (and possibly naïve)
> question :
>
> - I do not consider recursive lists as the perfect
> implementation for my problem
>
> - I do not care about efficiency
>
> - I do not want to use an ad hoc mutable/lazy list datatype
> (unless I’ve also a conversion function toward standard lists)
>
> - I do not want to use Obj or other similar tricks
>
> It’s just that I’m curious whether or not what I’m trying to achieve
> is possible.
>
>
>
> Regards, Eric
>
It sometimes helps to read read the various libraries.
For example, this thing is a variation of Batteries.BatList.Append:
module Cycle = struct
type 'a mut_list = { hd: 'a; mutable tl: 'a list }
external inj : 'a mut_list -> 'a list = "%identity"
external jni : 'a list -> 'a mut_list = "%identity"
let docycle = function (* copy and convert to circular *)
| [] -> raise (Failure"Cannot make [] circular")
| h0 :: t ->
let r = { hd = h0; tl = [] } in
let rec loop dst = function
| [] -> dst.tl <- inj r; dst.tl
| h :: t ->
let cell = { hd = h; tl = [] } in
dst.tl <- inj cell;
loop cell t
in loop r t
let docycle = function (* convert to circular in place *)
| [] -> raise (Failure"Cannot make [] circular")
| ll ->
let rec aux dst =
if dst.tl = [] then begin (* at end : *)
dst.tl <- ll; (* replace last cell with ll *)
List.tl (inj dst) (* and rotate to begin *)
end else aux (jni (dst.tl)); (* advance *)
in aux (jni ll)
end
let a = Cycle.docycle (ExtLib.String.explode "1234")
let _ = let open Printf in List.iter print_char (ExtLib.List.take 50 a)
$peter@ubuntu:~/ocaml/dink$ ./a.out
12341234123412341234123412341234123412341234123412peter@ubuntu:~/ocaml/dink$
next prev parent reply other threads:[~2012-12-21 19:55 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-18 8:37 Eric Jaeger
2012-12-21 19:55 ` Peter Frey [this message]
2012-12-22 18:10 ` Philippe Wang
[not found] ` <50D59147.3000201@ssi.gouv.fr>
2012-12-28 1:41 ` Peter Frey
2012-12-28 9:37 ` Arkady Andrukonis
2012-12-28 12:21 ` Philippe Wang
2012-12-28 12:30 ` Philippe Wang
2012-12-28 15:22 ` Didier Cassirame
2013-01-04 0:45 ` Francois Berenger
[not found] <50d02b72.7155c20a.1dbf.4e2fSMTPIN_ADDED_BROKEN@mx.google.com>
2012-12-18 9:35 ` Gabriel Scherer
[not found] <50d02b65.6c4cb40a.66ab.4256SMTPIN_ADDED_BROKEN@mx.google.com>
2012-12-18 11:21 ` Julien Blond
2012-12-18 13:13 ` Eric Jaeger
[not found] ` <50d06c18.0f5cc20a.16d8.ffff8b8cSMTPIN_ADDED_BROKEN@mx.google.com>
2012-12-19 16:45 ` Lukasz Stafiniak
[not found] <50d02b62.827bc20a.6f6e.65b8SMTPIN_ADDED_BROKEN@mx.google.com>
2012-12-19 22:23 ` Philippe Wang
2012-12-19 23:50 ` Jeremy Yallop
2012-12-20 15:24 ` Ashish Agarwal
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=BLU0-SMTP100167F680F50824B119393A3360@phx.gbl \
--to=pjfrey@sympatico.ca \
--cc=caml-list@inria.fr \
--cc=eric.jaeger@ssi.gouv.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