Re: List.flatten....

"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Sat, 2 Apr 2016 19:58:51 +0000 (UTC)
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
The implementation of ocaml lists tells the real difference between how it will perform in the real world. OCaml lists are singly linked lists. List.append must traverse to the end of the first list and link the tail with the other list to complete; it is O(N) where N is the length of the first list. The const operator is O(1), and in nearly every function implementation requiring concatenation of lists you can almost ways break down the first list and prepend it instead of appending whole lists. Some cleverness might be required, but in such cases don't forget that List.rev can be used after all the processing is done, and it won't change the order of magnitude if your function is O(N) because List.rev is O(N) too. 

    On Saturday, April 2, 2016 10:07 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
 

     Sure can.... 'a -> 'a list -> a' list is for List.cons
'a list -> 'a list -> a' list is for List.append
I think those 2 functions start to overlap however if you're working with a list of lists.  Well "overlap" could be a poor choice of words, but what if 'a refers to a list and 'a list -> refers to a list of lists, so then you would have something like:
[1; 2; 3; 4] :: [[5; 6; 7; 8]] ;;   
The result is a list of lists or nested lists.
A professor of mine is studying Haskell.  I asked him if he liked it, to which he replied.... "It's very LISTY".  And I think LISP is really an acronym for LISt Processing.  It seems that this is a hallmark of most ( or all? ) functional programming languages.  A big emphasis on lists and list manipulations.  Of course how do you define a list?  In some languages the words "array" and "list" are interchangeable.  ( I think Python for example does have arrays, but in general in Python arrays and lists are interchangeable. )  I read somewhere that Ocaml lists have more in common with Java linked lists than Java arrays.
Okay.... I'm rambling!  I better shut up!  I hope everyone on the list has a great weekend.
Best,
Douglas.  
On Sat, Apr 2, 2016 at 5:33 AM, 'Mr. Herr' [email protected] [ocaml_beginners] <[email protected]> wrote:

      
 
 On 02.04.2016 07:32, Douglas Lewit [email protected] [ocaml_beginners] wrote:
  
    
 I think I read somewhere that the cons operator :: is more efficient than the append operator @, so whenever possible it's better to use cons rather than append.    
 # List.cons ;;  (* this is :: *) - : 'a -> 'a list -> 'a list = <fun> # List.append ;;  (* this is @ *) - : 'a list -> 'a list -> 'a list = <fun> #  
 there is a tiny little difference, can you find it?
 
 /Str.
 
    

  #yiv5215973767 #yiv5215973767 -- #yiv5215973767ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv5215973767 #yiv5215973767ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv5215973767 #yiv5215973767ygrp-mkp #yiv5215973767hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv5215973767 #yiv5215973767ygrp-mkp #yiv5215973767ads {margin-bottom:10px;}#yiv5215973767 #yiv5215973767ygrp-mkp .yiv5215973767ad {padding:0 0;}#yiv5215973767 #yiv5215973767ygrp-mkp .yiv5215973767ad p {margin:0;}#yiv5215973767 #yiv5215973767ygrp-mkp .yiv5215973767ad a {color:#0000ff;text-decoration:none;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ygrp-lc {font-family:Arial;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ygrp-lc #yiv5215973767hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ygrp-lc .yiv5215973767ad {margin-bottom:10px;padding:0 0;}#yiv5215973767 #yiv5215973767actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv5215973767 #yiv5215973767activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv5215973767 #yiv5215973767activity span {font-weight:700;}#yiv5215973767 #yiv5215973767activity span:first-child {text-transform:uppercase;}#yiv5215973767 #yiv5215973767activity span a {color:#5085b6;text-decoration:none;}#yiv5215973767 #yiv5215973767activity span span {color:#ff7900;}#yiv5215973767 #yiv5215973767activity span .yiv5215973767underline {text-decoration:underline;}#yiv5215973767 .yiv5215973767attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv5215973767 .yiv5215973767attach div a {text-decoration:none;}#yiv5215973767 .yiv5215973767attach img {border:none;padding-right:5px;}#yiv5215973767 .yiv5215973767attach label {display:block;margin-bottom:5px;}#yiv5215973767 .yiv5215973767attach label a {text-decoration:none;}#yiv5215973767 blockquote {margin:0 0 0 4px;}#yiv5215973767 .yiv5215973767bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv5215973767 .yiv5215973767bold a {text-decoration:none;}#yiv5215973767 dd.yiv5215973767last p a {font-family:Verdana;font-weight:700;}#yiv5215973767 dd.yiv5215973767last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv5215973767 dd.yiv5215973767last p span.yiv5215973767yshortcuts {margin-right:0;}#yiv5215973767 div.yiv5215973767attach-table div div a {text-decoration:none;}#yiv5215973767 div.yiv5215973767attach-table {width:400px;}#yiv5215973767 div.yiv5215973767file-title a, #yiv5215973767 div.yiv5215973767file-title a:active, #yiv5215973767 div.yiv5215973767file-title a:hover, #yiv5215973767 div.yiv5215973767file-title a:visited {text-decoration:none;}#yiv5215973767 div.yiv5215973767photo-title a, #yiv5215973767 div.yiv5215973767photo-title a:active, #yiv5215973767 div.yiv5215973767photo-title a:hover, #yiv5215973767 div.yiv5215973767photo-title a:visited {text-decoration:none;}#yiv5215973767 div#yiv5215973767ygrp-mlmsg #yiv5215973767ygrp-msg p a span.yiv5215973767yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv5215973767 .yiv5215973767green {color:#628c2a;}#yiv5215973767 .yiv5215973767MsoNormal {margin:0 0 0 0;}#yiv5215973767 o {font-size:0;}#yiv5215973767 #yiv5215973767photos div {float:left;width:72px;}#yiv5215973767 #yiv5215973767photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv5215973767 #yiv5215973767photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv5215973767 #yiv5215973767reco-category {font-size:77%;}#yiv5215973767 #yiv5215973767reco-desc {font-size:77%;}#yiv5215973767 .yiv5215973767replbq {margin:4px;}#yiv5215973767 #yiv5215973767ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv5215973767 #yiv5215973767ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv5215973767 #yiv5215973767ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv5215973767 #yiv5215973767ygrp-mlmsg select, #yiv5215973767 input, #yiv5215973767 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv5215973767 #yiv5215973767ygrp-mlmsg pre, #yiv5215973767 code {font:115% monospace;}#yiv5215973767 #yiv5215973767ygrp-mlmsg * {line-height:1.22em;}#yiv5215973767 #yiv5215973767ygrp-mlmsg #yiv5215973767logo {padding-bottom:10px;}#yiv5215973767 #yiv5215973767ygrp-msg p a {font-family:Verdana;}#yiv5215973767 #yiv5215973767ygrp-msg p#yiv5215973767attach-count span {color:#1E66AE;font-weight:700;}#yiv5215973767 #yiv5215973767ygrp-reco #yiv5215973767reco-head {color:#ff7900;font-weight:700;}#yiv5215973767 #yiv5215973767ygrp-reco {margin-bottom:20px;padding:0px;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ov li a {font-size:130%;text-decoration:none;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv5215973767 #yiv5215973767ygrp-sponsor #yiv5215973767ov ul {margin:0;padding:0 0 0 8px;}#yiv5215973767 #yiv5215973767ygrp-text {font-family:Georgia;}#yiv5215973767 #yiv5215973767ygrp-text p {margin:0 0 1em 0;}#yiv5215973767 #yiv5215973767ygrp-text tt {font-size:120%;}#yiv5215973767 #yiv5215973767ygrp-vital ul li:last-child {border-right:none !important;}#yiv5215973767