From: Pierre Weis <Pierre.Weis@inria.fr>
To: proff@iq.org (Julian Assange)
Cc: caml-list@inria.fr
Subject: Re: addressable_to_list
Date: Fri, 28 Jul 2000 16:46:20 +0200 (MET DST) [thread overview]
Message-ID: <200007281446.QAA22344@pauillac.inria.fr> (raw)
In-Reply-To: <wx66ptyo6z.fsf@foo.iq.org> from Julian Assange at "Jul 27, 100 00:13:24 am"
> The following ocaml function converts a string to a list:
> (which btw, I guess should be in the String module)
This has been discussed many times in this list. We always ended up with
the following conclusion:
1) This function is trivial to implement
2) This way of dealing with strings is inefficient and should not be
encouraged in the core library.
[...]
> However, what I'd like to be able to do this for any countable thing.
>
> e.g
>
> let nthable_to_list s fnth flen =
> let len = flen s in
> let rec scan n =
> if n < len then
> fnth s n :: scan (n + 1)
> else
> []
> in
> scan len
>
> Then one can of course simulate the first code snippit with:
>
> let string_of_list s = nthable_to_list s (fun s n -> s.[n]) (fun s -> String.length s)
>
> I know how this is done in Haskell, but how do something like it in O'caml?
What's the problem with your code ? It looks pretty neat to me :)
[...]
>
> While I'm on the subject of array initialisers, the
> new safe array creation could be improved. From memory,
> it takes a function which takes an int and
> returns an initialisation element. It would be better
> if the counting was left upto the user supplied function
> and the output was fed back into the input. e.g like foldr.
> e.g
>
> ('a -> 'b * 'a) -> 'b array
>
> 1.. initialisation would then look like
>
> (fun x -> x, x+1)
This has been discussed in a thread named reference initialization
2 months ago. We ended up with a pretty general higher-order
initialization function:
(*
[initialize n f] returns a fresh array of length [n],
with elements initialized by function [f].
All the elements of the new array must be assigned once and only
once by the function [f]. [f] received two functions as arguments,
one to access elements of the new array, and the other to set the
elements of the new array. [f] can access to element [i] of the new
array provided [f] has already properly initialized element [i].
Raise [Not_yet_initialized i] if element [i] is accessed before being
assigned.
Raise [Already_initialized i] if element [i] is assigned twice.
Raise [Never_initialized i] if element [i] has never been assigned at
the end of initialization.
[Array.initialize n f] uses [2 n] words of heap space.
*)
exception Not_yet_initialized of int;;
exception Already_initialized of int;;
exception Never_initialized of int;;
(*
val initialize :
int -> ((int -> 'a) -> (int -> 'a -> unit) -> unit) -> 'a array
*)
let initialize n f =
if n = 0 then [||] else
let init_v = Array.make n false in
let v = ref [||] in
let get i = if init_v.(i) then !v.(i) else raise (Not_yet_initialized i) in
let set i ei =
if !v = [||] then v := Array.make n ei;
if not init_v.(i) then (!v.(i) <- ei; init_v.(i) <- true) else
raise (Already_initialized i) in
(f get set : unit);
for i = 0 to n - 1 do if not init_v.(i) then raise (Never_initialized i) done;
!v;;
>From this initialization function, you can easily define your array
initializer:
let ja_initialize n f =
let ja_init get set =
let rec init i =
let e, j = f i in
set i e;
init j in
init 0 in
initialize n ja_init;;
val ja_initialize : int -> (int -> 'a * int) -> 'a array = <fun>
You'll still have the good properties of complete and unique
initialization that initialize ensures.
Pierre Weis
INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/
prev parent reply other threads:[~2000-07-28 14:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-07-26 14:13 addressable_to_list Julian Assange
2000-07-26 21:35 ` addressable_to_list Julian Assange
2000-07-28 14:46 ` Pierre Weis [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=200007281446.QAA22344@pauillac.inria.fr \
--to=pierre.weis@inria.fr \
--cc=caml-list@inria.fr \
--cc=proff@iq.org \
/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