caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jeremy Fincher" <tweedgeezer@hotmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] function
Date: Fri, 06 Dec 2002 02:38:37 -0500	[thread overview]
Message-ID: <F82C81TbdkSmLAztxc100017fd9@hotmail.com> (raw)

I prefer this:

(* Why in the world aren't these included in the standard library? *)
let string_foldr f init s =
  let rec aux i x =
    if i < 0 then
       x
    else
       aux (i-1) (f (String.get s i) x)
  in
    aux (String.length s - 1) init

let explode = string_foldr (fun c acc -> c :: acc) []
(* End "Why in the world?" comment *)

let digitToInt c =
  match c with
    | '0' -> 0
    | '1' -> 1
    | '2' -> 2
    | '3' -> 3
    | '4' -> 4
    | '5' -> 5
    | '6' -> 6
    | '7' -> 7
    | '8' -> 8
    | '9' -> 9
    | _   -> failwith "invalid digit"

let f l = List.map digitToInt (List.flatten (List.map explode l))

(* Or, to be more efficient, without constructing the intermediate lists via 
explode. *)
let f' l = List.fold_right (fun s l1 -> string_foldr (fun c l2 -> digitToInt 
c :: l2) l1 s) l []
(* Out of curiosity, why does fold_right take the initial value after the 
list it operates on? *)

I don't think users would be as tempted to write imperative, possibly 
inefficient code working with strings if the String module included better 
iterators such a fold_right and fold_left (and maybe even map, since strings 
are mutable in O'Caml and it could thus be done efficiently).

Jeremy

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online 
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2002-12-06 19:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-06  7:38 Jeremy Fincher [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-12-02 15:55 altavillasalvatore
2002-12-02 16:29 ` sebastien FURIC
2002-12-02 17:35 ` Oleg
2002-12-03 23:22 ` Issac Trotts
2002-12-05 10:00   ` Pierre Weis
2002-12-05 20:24     ` Issac Trotts
2002-12-05 23:00       ` Oleg
2002-12-06 21:31         ` Issac Trotts
2002-12-07 10:28           ` Xavier Leroy
2002-12-07 17:31             ` Oleg
2002-12-05 20:46     ` Oleg
2002-12-05 21:06       ` Pal-Kristian Engstad

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=F82C81TbdkSmLAztxc100017fd9@hotmail.com \
    --to=tweedgeezer@hotmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).