caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Eric C. Cooper" <ecc@cmu.edu>
To: Richard Jones <rich@annexia.org>
Cc: Aaron Bohannon <bohannon@cis.upenn.edu>,
	caml-list@yquem.inria.fr, ocaml-lib-devel@lists.sourceforge.net
Subject: Re: [Caml-list] String to list to string
Date: Tue, 15 Feb 2005 08:34:03 -0500	[thread overview]
Message-ID: <20050215133403.GB18971@localhost> (raw)
In-Reply-To: <20050215103316.GA21083@furbychan.cocan.org>

On Tue, Feb 15, 2005 at 10:33:16AM +0000, Richard Jones wrote:
> If you can suggest suitable fold_left and fold_right functions, then
> they can be added to ExtLib.

Here's what I use:

val string_fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
val string_fold_right : (char -> 'a -> 'a) -> 'a -> string -> 'a

let string_fold_left f init str =
  let n = String.length str in
  let rec loop i result =
    if i = n then result
    else loop (i + 1) (f result str.[i])
  in
  loop 0 init

let string_fold_right f init str =
  let n = String.length str in
  let rec loop i result =
    if i = 0 then result
    else
      let i' = i - 1 in
      loop i' (f str.[i'] result)
  in
  loop n init

-- 
Eric C. Cooper          e c c @ c m u . e d u


  reply	other threads:[~2005-02-15 13:34 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-07  2:24 Fwd: Re: [Caml-list] The boon of static type checking Jon Harrop
2005-02-07  2:55 ` skaller
2005-02-10  2:10 ` String to list to string Juancarlo Añez
2005-02-10  2:27   ` [Caml-list] " William D.Neumann
2005-02-10  3:24     ` Erik de Castro Lopo
2005-02-10  6:31       ` Radu Grigore
2005-02-10  6:52         ` Erik de Castro Lopo
2005-02-10  3:41   ` Jon Harrop
2005-02-15  1:16     ` Aaron Bohannon
2005-02-15 10:33       ` Richard Jones
2005-02-15 13:34         ` Eric C. Cooper [this message]
2005-02-10 10:09   ` Richard Jones
2005-02-10 19:19     ` Juancarlo Añez
     [not found]     ` <E1CzJqb-00031c-00@furbychan.cocan.org>
2005-02-10 19:41       ` Richard Jones
2005-02-10 17:58   ` brogoff
2005-02-10 18:35 Harrison, John R
2005-02-10 19:28 ` Jon Harrop
2005-02-11  1:22   ` skaller
2005-02-11  2:05     ` John Prevost
2005-02-10 19:32 ` brogoff
2005-02-10 19:51 Harrison, John R
2005-02-10 21:18 Harrison, John R

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=20050215133403.GB18971@localhost \
    --to=ecc@cmu.edu \
    --cc=bohannon@cis.upenn.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=ocaml-lib-devel@lists.sourceforge.net \
    --cc=rich@annexia.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;
as well as URLs for NNTP newsgroup(s).