<?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/5581c0bd84fc85a1433cc7954b60ed48"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2002-12-06T21:30:52"
  subject="Re: [Caml-list] function"
  prev="2002/12/dc088b981df0d96498a14857f400b381"
  next="2002/12/458ac04874c1d4210349ca4aef0cefb1"
  prev-in-thread="2002/12/6a861191e568645e8947da7ef9a991bc"
  next-in-thread="2002/12/458ac04874c1d4210349ca4aef0cefb1"
  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>

Oleg wrote:
&gt; On Thursday 05 December 2002 03:24 pm, Issac Trotts wrote:
&gt; &gt;   List.concat (List.map to_list2 strs);;
&gt; 
&gt;     ^^^^^^^^^
&gt; 
&gt; &gt; &gt; 2)  What is the complexity of your function f ?
&gt; &gt;
&gt; &gt; The new ones have linear time complexity w.r.t. the number of
&gt; &gt; characters.  The old one has quadratic time complexity.
&gt; 
&gt; Issac
&gt; 
&gt; I haven't analyzed your whole functions, but List.flatten'ing alone is 
&gt; O(S*L^2), so while it may be linear WRT the number of characters in one 
&gt; string, it's not necessarily linear WRT the total number of characters. See 
&gt; my version of f for O(L*S) time.
&gt; 
&gt; Cheers
&gt; Oleg

Thanks for pointing this out.

Here's flatten, from list.ml:

  let rec flatten = function
      [] -&gt; []
    | l::r -&gt; l @ flatten r

  let concat = flatten

Here's (@), from pervasives.ml : 

  let rec (@) l1 l2 =
    match l1 with
      [] -&gt; l2
    | hd :: tl -&gt; hd :: (tl @ l2)

If the given list has L elements, each with S items, then flatten should 
O((L*S)*L) = O(S*L^2) time, since you have to keep on churning through
every single element in the ever-expanding l at every recursive flatten 
call.  That's too bad.

Here's an experiment I tried:

$ cat ftime.ml 

  let rec make_list_of_lists = function 
    | 0 -&gt; []
    | n -&gt; [1;2;3;4;5;6;7;8] :: make_list_of_lists(n-1)

  let _ = 
    let n = int_of_string(Sys.argv.(1)) in
    let lol = make_list_of_lists n in
    ignore(List.flatten(lol))
  ;;

$ ocamlopt -o ftime ftime.ml
$ for (( i=0; $i&lt;100; i++ )) 
  do 
    /usr/bin/time -o data -a -f "%U" ./ftime "$i"000  
  done 
$ gnuplot
  &gt; plot 'data'

I guess it looks linear because of the small input size.
This gives me the impression that List.flatten is practical,
at least for small data sets.

Issac


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

