Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Julian Assange <proff@iq.org>
To: caml-list@inria.fr
Cc: proff@iq.org
Subject: addressable_to_list
Date: 27 Jul 2000 00:13:24 +1000	[thread overview]
Message-ID: <wx66ptyo6z.fsf@foo.iq.org> (raw)


The following ocaml function converts a string to a list:
(which btw, I guess should be in the String module)

let string_to_list s =
  let len = String.length s in
  let rec scan n =
    if n < len then
      s.[n] :: scan (n + 1)
    else
      []
  in
    scan len

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?

A couple of useful string functions that are missing:

        conversion, strings <-> bigarrays of chars a bigarry like
        create function, which takes a function argument. (regular
        Array's also have the same problem). String has functions to
        create an empty, constant filled string, or an uninitialised
        string. This is pretty important, because Digest for instance,
        only takes strings.

        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)


Cheers,
Julian.



             reply	other threads:[~2000-07-28 13:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-26 14:13 Julian Assange [this message]
2000-07-26 21:35 ` addressable_to_list Julian Assange
2000-07-28 14:46 ` addressable_to_list Pierre Weis

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=wx66ptyo6z.fsf@foo.iq.org \
    --to=proff@iq.org \
    --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