I found myself wanting a map that looks at neighboring elements. This is where I used explicit recursion the most. Something like this:<br><br>f [] = []<br>f ((Foo a) : (Bar b) : xs)<br>&nbsp;&nbsp; | fooBar a b = Foo a : f xs<br>&nbsp;&nbsp; | otherwise = Bar b : f xs
<br><br>This is almost a map. A variation is when filtering and you want some look-ahead to make the filtering decision. There&#39;s probably a good way to do this I&#39;m not aware of.<br><br>Johan<br><br>On 7/17/07, David F. Place &lt;
<a href="mailto:d@vidplace.com">d@vidplace.com</a>&gt; wrote:<br>&gt; You hardly ever need to use explicit recursion in Haskell.&nbsp;&nbsp;Every<br>&gt; useful way of doing recursion has already been captured in some<br>&gt; higher order function.&nbsp;&nbsp;For example here is your subarrays
<br>&gt; implemented using unfoldr:<br>&gt; <br>&gt; subarrays xs = concat $ unfoldr f xs<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f [] = Nothing<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f xs = Just&nbsp;&nbsp;( [ys | n &lt;- [1..length xs], ys &lt;- [(take n
<br>&gt; xs)]], tail xs)<br>&gt; <br>&gt; On Jul 17, 2007, at 4:26 PM, James Hunt wrote:<br>&gt; <br>&gt; &gt; Hi,<br>&gt; &gt;<br>&gt; &gt; As a struggling newbie, I&#39;ve started to try various exercises in<br>&gt; &gt; order to improve. I decided to try the latest Ruby Quiz (http://
<br>&gt; &gt; <a href="http://www.rubyquiz.com/quiz131.html">www.rubyquiz.com/quiz131.html</a>) in Haskell. Would someone be kind<br>&gt; &gt; enough to cast their eye over my code? I get the feeling there&#39;s a<br>&gt; &gt; better way of doing it!
<br>&gt; &gt;<br>&gt; &gt; subarrays :: [a] -&gt; [[a]]<br>&gt; &gt; subarrays [] = [[]]<br>&gt; &gt; subarrays xs = (sa xs) ++ subarrays (tail xs)<br>&gt; &gt;&nbsp;&nbsp;where sa xs = [ys | n &lt;- [1..length xs], ys &lt;- [(take n xs)]]
<br>&gt; &gt;<br>&gt; &gt; maxsubarrays :: [Integer] -&gt; [Integer]<br>&gt; &gt; maxsubarrays xs = msa [] (subarrays xs)<br>&gt; &gt;&nbsp;&nbsp;where<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;msa m [] = m<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;msa m (x:xs)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| sum x &gt; sum m = msa x xs
<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| otherwise&nbsp;&nbsp;&nbsp;&nbsp; = msa m xs<br>&gt; &gt;<br>&gt; &gt; --for testing: should return [2, 5, -1, 3]<br>&gt; &gt; main = maxsubarrays [-1, 2, 5, -1, 3, -2, 1]<br>&gt; &gt;<br>&gt; &gt; I&#39;ve read tutorials about the syntax of Haskell, but I can&#39;t seem
<br>&gt; &gt; to find any that teach you how to really &quot;think&quot; in a Haskell way.<br>&gt; &gt; Is there anything (books, online tutorials, exercises) that anyone<br>&gt; &gt; could recommend?<br>&gt; &gt;<br>&gt; &gt; Thanks,
<br>&gt; &gt; James<br>&gt; &gt; _______________________________________________<br>&gt; &gt; Haskell-Cafe mailing list<br>&gt; &gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; &gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>&gt; <br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;___________________<br>&gt; (---o-------o-o-o---o-o-o----(<br>&gt; David F. Place<br>&gt; mailto:<a href="mailto:d@vidplace.com">d@vidplace.com</a>
<br>&gt; <br>&gt; <br>&gt; _______________________________________________<br>&gt; Haskell-Cafe mailing list<br>&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>&gt; <br><br>