It would be nice if it was possible to capture this kind of behavior in a high order function just like map though. I guess the problem is that the function to map will take different number of arguments depending on the use case.
<br><br>lookAtTwo a b = ...<br><br>lookAtThree a b c = ...<br><br>map' :: (a -> ... -> b) -> [a] -> [b]<br><br>The parameter take a variable number of parameters.<br><br>Note: I don't know if there is a sensible way to write map' at all. Perhaps explicit recursion is better in this case.
<br><br><div><span class="gmail_quote">On 7/18/07, <b class="gmail_sendername">apfelmus</b> <<a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Johan Tibell wrote:<br>> I found myself wanting a map that looks at neighboring elements. This is<br>> 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<br>> look-ahead to make the filtering decision. There's probably a good way
<br>> to do this I'm not aware of.<br><br>There are some cases missing, like<br><br> f [x] = ??<br> f (Bar a : Foo b : xs) = ??<br><br>A better example is probably<br><br> takeUntilConvergence epsilon (x:x':xs)
<br> | abs (x-x') < epsilon = [x]<br> | otherwise = x:takeUntilConvergence epsilon (x':xs)<br><br>useful for numeric iterations like<br><br> sqrt a = last $ takeUntilConvergence (1e-10)<br> $ iterate (\x -> (x+a/x)/2) 1
<br><br>Another way to implement takeUntilConvergence is to zip the list<br>with its tail:<br><br> takeUntilConvergence epsilon xs =<br> fst . head . dropUntil ((< epsilon) . snd)<br> $ zipWith (\x x' -> (x,abs(x-x')) xs (tail xs)
<br><br><br>Regards,<br>apfelmus<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></blockquote></div><br>