Sounds like what I want. I'll give it a try. Thanks.<br><br><div><span class="gmail_quote">On 7/18/07, <b class="gmail_sendername">Tillmann Rendel</b> <<a href="mailto:rendel@rbg.informatik.tu-darmstadt.de">rendel@rbg.informatik.tu-darmstadt.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>If you want to map over all elements, but need to look ahead in the<br>mapped function, you can map over the tails:
<br><br> map' :: ([a] -> b) -> [a] -> b<br> map' f = map f . tails<br><br>f should be something like<br> f (a:b:c:_) = ...<br><br><br>If you want to handle groups of n elements together, producing only one
<br>element per group, you can use unfoldr with splitAt:<br><br> map'' :: Int -> ([a] -> b) -> [a] -> [b]<br> map'' n f =<br> map f . unfoldr (((not . null . fst) `guarding`) . splitAt n)
<br><br> guarding p x = guard (p x) >> return x<br><br><br>If you want to decide in the mapped function how many elements to<br>consume, you can use unfoldr directly.<br><br> Tillmann Rendel<br></blockquote></div>
<br>