From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA10346; Fri, 6 Dec 2002 20:58:20 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA11475 for caml-list@pauillac.inria.fr; Fri, 6 Dec 2002 20:58:20 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA22580 for ; Fri, 6 Dec 2002 08:38:40 +0100 (MET) Received: from hotmail.com (f82.law15.hotmail.com [64.4.23.82]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gB67cdX23990 for ; Fri, 6 Dec 2002 08:38:39 +0100 (MET) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Thu, 5 Dec 2002 23:38:38 -0800 Received: from 140.254.114.48 by lw15fd.law15.hotmail.msn.com with HTTP; Fri, 06 Dec 2002 07:38:37 GMT X-Originating-IP: [140.254.114.48] From: "Jeremy Fincher" To: caml-list@inria.fr Subject: [Caml-list] function Date: Fri, 06 Dec 2002 02:38:37 -0500 Mime-Version: 1.0 Content-Type: text/plain; format=flowed Message-ID: X-OriginalArrivalTime: 06 Dec 2002 07:38:38.0036 (UTC) FILETIME=[7D74D140:01C29CFA] Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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