caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] function
@ 2002-12-06  7:38 Jeremy Fincher
  0 siblings, 0 replies; 13+ messages in thread
From: Jeremy Fincher @ 2002-12-06  7:38 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 13+ messages in thread
* [Caml-list] function
@ 2002-12-02 15:55 altavillasalvatore
  2002-12-02 16:29 ` sebastien FURIC
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: altavillasalvatore @ 2002-12-02 15:55 UTC (permalink / raw)
  To: caml-list

Hi all,
I would want to know if a function exists that allows me to make this:

["123";"45";"678"]  ->  [1;2;3;4;5;6;7;8;].

you excuse me for the banal demand!!

 I still attend to enter in the ocaml-beginners.

Regards



-------------------
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


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2002-12-07 17:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-06  7:38 [Caml-list] function Jeremy Fincher
  -- 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

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).