<br><br><div class="gmail_quote">2009/2/7 Cory Knapp <span dir="ltr">&lt;<a href="mailto:thestonetable@gmail.com">thestonetable@gmail.com</a>&gt;</span><br><div><br>&lt;nice explanation on : operator - always useful&gt; <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Does that help, or did I miss the point?<br>
<br></blockquote><div>Ok, I&#39;ll try to be more clear with an example: the following function - which surely can be written in better way - takes a list and a predicate and builds two lists. a list of lists of consecutive elements that satisfy the predicate and a list of separators, i.e. of elements that does not satisfy the predicate.&nbsp; <br>
</div></div>That is: groups&nbsp; odd&nbsp; [1,3,2,5,9,6] -&gt;&nbsp;&nbsp; [[1,3],[5,9]],&nbsp; [2,6]<br>The function uses another function, groupWhile, which works like takeWhile but returns also the rest of the list.<br><br>Now, from the way the function works, it shoud build the two result lists by appending new elements to them.<br>
But, as you said, appending is expensive in haskell, so instead I build the lists using (:), which gives me the list<br>in reverse order, and then I use the &#39;final step&#39; of the function - the base case of the recursion - to reverse the<br>
result. If I understand lazyness, this should be a low-cost operation because it does not actual build a reversed<br>list, but&nbsp; only make the list to be &#39;consumed&#39; from the tail.<br><br>Now, this trick is not mine: I read it somewhere on internet (I can&#39;tremember where), so I was asking if it is classified among the &#39;neat tricks&#39; or among the &#39;ugly hacks&#39; :-)<br>
<br>Here is the code I was referring to:<br><br><br>groups :: (a-&gt;Bool)-&gt; [a] -&gt; ([[a]],[a])<br>groups f lst = groups_ f ([],[]) lst where<br>&nbsp;&nbsp;&nbsp; groups_ f (gs,seps) [] = (reverse gs, reverse seps)<br>&nbsp;&nbsp;&nbsp; groups_ f (gs,seps) [a] = if (f a) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; then ( reverse ( [a]:gs), reverse seps )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else ( reverse gs , reverse (a:seps) )<br>&nbsp;&nbsp;&nbsp; groups_ f (gs, seps) lst = groups_ f (g:gs, r:seps) est <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (g, (r:est)) = groupWhile f lst<br><br>Ciao<br>-------<br>FB<br><br><br>