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 WAA11904; Thu, 5 Dec 2002 22:04:47 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA11991 for ; Thu, 5 Dec 2002 22:04:46 +0100 (MET) Received: from slave-dog.naughtydog.com (naughtydog200.brandx.net [209.55.75.200]) by nez-perce.inria.fr (8.11.1/8.11.1) with SMTP id gB5L4i106639; Thu, 5 Dec 2002 22:04:45 +0100 (MET) Received: from SMTP agent by mail gateway Thu, 05 Dec 2002 13:06:55 -0800 Received: from katn-dog (engstad@katn-dog.naughtydog.com [10.0.0.90]) by slave-dog.naughtydog.com (SGI-8.9.3/8.9.3) with ESMTP id NAA96638; Thu, 5 Dec 2002 13:06:45 -0800 (PST) Content-Type: text/plain; charset="iso-8859-1" From: Pal-Kristian Engstad Organization: Naughty Dog, Inc. To: Oleg , Pierre Weis , ijtrotts@ucdavis.edu (Issac Trotts) Subject: Re: [Caml-list] function Date: Thu, 5 Dec 2002 13:06:44 -0800 User-Agent: KMail/1.4.3 Cc: caml-list@inria.fr References: <200212051000.LAA22469@pauillac.inria.fr> <200212051546.56416.oleg_inconnu@myrealbox.com> In-Reply-To: <200212051546.56416.oleg_inconnu@myrealbox.com> MIME-Version: 1.0 Message-Id: <200212051306.44106.engstad@naughtydog.com> Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by slave-dog.naughtydog.com id NAA96638 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thursday 05 December 2002 12:46 pm, Oleg wrote: > To help the original poster earn extra credit, I'll write the function = in > the three languages I [sort of] know: > > typedef std::list LS; > typedef std::list LI; > > void f(LS& input, LI& output) { > for(LS::iterator i =3D input.begin(); i !=3D input.end(); ++i) > for(char* p =3D *i; *p !=3D '\0'; ++p) > output.push_back(int(*p) - int('0')); > } Isn't there a foreach() algorithm in STL? > let rec f_aux lst i acc =3D > match lst with > > | [] -> acc > | x :: y when i =3D String.length x -> f_aux y 0 acc > | x :: y -> f_aux lst (i + 1) (digit x.[i] :: acc) > > let f lst =3D List.rev (f_aux lst 0 []) Ah, the use of List.rev... :-) Of course, without it, it is hard. Here's = my two=20 attempts: let f4 strs =3D=20 let to_ord c =3D int_of_char c - int_of_char '0' in let rec strings_to_list =3D function | [] -> [] | str :: strs -> let rec to_list s k n =3D=20 if k =3D n then strings_to_list strs else to_ord s.[k] :: to_list s (k= + 1) n in to_list str 0 (String.length str)=20 in strings_to_list strs This one is unfortunately not tail recursive, but it is neat in the sense= that it doesn't use any library function except int_of_char. let f5 strs =3D=20 let to_ord c =3D int_of_char c - int_of_char '0' in let rec strings_to_list acc =3D function | [] -> acc | str :: strs -> let rec to_list acc s k n =3D if k =3D n then strings_to_list acc strs else to_list (to_ord s.[k] ::= acc) s (k + 1) n in to_list acc str 0 (String.length str) in List.rev(strings_to_list [] strs) Equivalent to Oleg's I believe? PKE. --=20 _ =20 \`. P=E5l-Kristian Engstad, Senior Software Engineer, \ `| Naughty Dog, Inc., 1315 3rd Street Promenade,=20 __\ |`. Santa Monica, CA 90046, USA. (310) 752-1000 x799.=20 / /o mailto:engstad@naughtydog.com http://www.naughtydog.com / '~ mailto:mrengstad@yahoo.com http://www.engstad.com / ,' Hang-gliding Rulez! ~' ------------------- 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