<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/12/87e12bef39b1bf9fc066aa51f3deb525"
  from="Pal-Kristian Engstad &lt;engstad@n...&gt;"
  author="Pal-Kristian Engstad"
  date="2002-12-05T21:04:47"
  subject="Re: [Caml-list] function"
  prev="2002/12/ae8917859b7799193678c7a51ce64247"
  next="2002/12/ec6b4886b2c39dc65c0dac1c458cf870"
  prev-in-thread="2002/12/ae8917859b7799193678c7a51ce64247"
  prev-thread="2002/12/e69ce402ec37766285461eb0c4583d17"
  next-thread="2002/12/838549db9eab58c179d31f32eefdf381"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] function">
<msg 
  url="2002/12/67f44329195eaf652b43093fd3ca01ba"
  from="altavillasalvatore@libero.it &lt;altavillasalvatore@l...&gt;"
  author="altavillasalvatore@libero.it"
  date="2002-12-02T15:55:08"
  subject="[Caml-list] function">
<msg 
  url="2002/12/aa10f5e9e0a36a35e6d25918c9404768"
  from="sebastien FURIC &lt;sebastien.furic@t...&gt;"
  author="sebastien FURIC"
  date="2002-12-02T16:30:12"
  subject="Re: [Caml-list] function">
</msg>
<msg 
  url="2002/12/01b22e6f3b5a5470860e77cba7c48d8e"
  from="Oleg &lt;oleg_inconnu@m...&gt;"
  author="Oleg"
  date="2002-12-02T17:35:49"
  subject="Re: [Caml-list] function">
</msg>
<msg 
  url="2002/12/9b00251c6816fbc02c4a06d09719d33e"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2002-12-03T23:21:13"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/3a29f1975beda5e8c692774800ed0d87"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2002-12-05T10:01:05"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/019830184ea14462c3df69900b5a0cda"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2002-12-05T20:24:25"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/6a861191e568645e8947da7ef9a991bc"
  from="Oleg &lt;oleg_inconnu@m...&gt;"
  author="Oleg"
  date="2002-12-05T23:01:21"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/5581c0bd84fc85a1433cc7954b60ed48"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2002-12-06T21:30:52"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/458ac04874c1d4210349ca4aef0cefb1"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-07T10:28:25"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/938da7b77f8b3b294f4ae5d5dade6d4f"
  from="Oleg &lt;oleg_inconnu@m...&gt;"
  author="Oleg"
  date="2002-12-07T17:32:03"
  subject="Re: [Caml-list] function">
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2002/12/ae8917859b7799193678c7a51ce64247"
  from="Oleg &lt;oleg_inconnu@m...&gt;"
  author="Oleg"
  date="2002-12-05T20:47:21"
  subject="Re: [Caml-list] function">
<msg 
  url="2002/12/87e12bef39b1bf9fc066aa51f3deb525"
  from="Pal-Kristian Engstad &lt;engstad@n...&gt;"
  author="Pal-Kristian Engstad"
  date="2002-12-05T21:04:47"
  subject="Re: [Caml-list] function">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
On Thursday 05 December 2002 12:46 pm, Oleg wrote:
&gt; To help the original poster earn extra credit, I'll write the function in
&gt; the three languages I [sort of] know:
&gt;
&gt; typedef std::list&lt;char*&gt; LS;
&gt; typedef std::list&lt;int&gt; LI;
&gt;
&gt; void f(LS&amp; input, LI&amp; output) {
&gt;     for(LS::iterator i = input.begin(); i != input.end(); ++i)
&gt;         for(char* p = *i; *p != '\0'; ++p)
&gt;             output.push_back(int(*p) - int('0'));
&gt; }

Isn't there a foreach() algorithm in STL?

&gt; let rec f_aux lst i acc =
&gt;     match lst with
&gt;
&gt;     | [] -&gt; acc
&gt;     | x :: y when i = String.length x -&gt; f_aux y 0 acc
&gt;     | x :: y -&gt; f_aux lst (i + 1) (digit x.[i] :: acc)
&gt;
&gt; let f lst = List.rev (f_aux lst 0 [])

Ah, the use of List.rev... :-) Of course, without it, it is hard. Here's my two 
attempts:

let f4 strs = 
  let to_ord c = int_of_char c - int_of_char '0' in
  let rec strings_to_list = function
    | [] -&gt; []
    | str :: strs -&gt;
	let rec to_list s k n = 
	  if k = 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) 
  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 = 
  let to_ord c = int_of_char c - int_of_char '0' in
  let rec strings_to_list acc = function
    | [] -&gt; acc
    | str :: strs -&gt;
	let rec to_list acc s k n =
	  if k = 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.
-- 
  _       
  \`.       Pål-Kristian Engstad, Senior Software Engineer,
   \ `|     Naughty Dog, Inc., 1315 3rd Street Promenade, 
  __\ |`.   Santa Monica, CA 90046, USA. (310) 752-1000 x799. 
    /  /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

</contents>

</message>

