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