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&#39; :: (a -&gt; ... -&gt; b) -&gt; [a] -&gt; [b]<br><br>The parameter take a variable number of parameters.<br><br>Note: I don&#39;t know if there is a sensible way to write map&#39; 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> &lt;<a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de</a>&gt; 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>&gt; I found myself wanting a map that looks at neighboring elements. This is<br>&gt; where I used explicit recursion the most. Something like this:<br>&gt;<br>&gt; f [] = []<br>&gt; f ((Foo a) : (Bar b) : xs)
<br>&gt;&nbsp;&nbsp; | fooBar a b = Foo a : f xs<br>&gt;&nbsp;&nbsp; | otherwise = Bar b : f xs<br>&gt;<br>&gt; This is almost a map. A variation is when filtering and you want some<br>&gt; look-ahead to make the filtering decision. There&#39;s probably a good way
<br>&gt; to do this I&#39;m not aware of.<br><br>There are some cases missing, like<br><br>&nbsp;&nbsp;f [x] = ??<br>&nbsp;&nbsp;f (Bar a : Foo b : xs) = ??<br><br>A better example is probably<br><br>&nbsp;&nbsp;takeUntilConvergence epsilon (x:x&#39;:xs)
<br>&nbsp;&nbsp;&nbsp;&nbsp;| abs (x-x&#39;) &lt; epsilon = [x]<br>&nbsp;&nbsp;&nbsp;&nbsp;| otherwise&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= x:takeUntilConvergence epsilon (x&#39;:xs)<br><br>useful for numeric iterations like<br><br>&nbsp;&nbsp; sqrt a = last $ takeUntilConvergence (1e-10)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ iterate (\x -&gt; (x+a/x)/2) 1
<br><br>Another way to implement&nbsp;&nbsp;takeUntilConvergence&nbsp;&nbsp;is to&nbsp;&nbsp;zip&nbsp;&nbsp;the list<br>with its tail:<br><br> takeUntilConvergence epsilon xs =<br>&nbsp;&nbsp; fst . head . dropUntil ((&lt; epsilon) . snd)<br>&nbsp;&nbsp; $ zipWith (\x x&#39; -&gt; (x,abs(x-x&#39;)) 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>