<br><div><span class="gmail_quote">On 8/9/07, <b class="gmail_sendername">Chad Scherrer</b> <<a href="mailto:chad.scherrer@gmail.com">chad.scherrer@gmail.com</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;">
<br>extract :: [Int] -> [a] -> [a]<br>extract = f 0<br> where<br> f _ _ [] = []<br> f _ [] _ = []<br> f k nss@(n:ns) (x:xs) = if n == k then x:f (k+1) ns xs<br> else f (k+1) nss xs
<br><br>This behaves roughly as<br>extract ns xs == map (xs !!) ns<br><br>except that it's a lot more efficient, and it still works if ns or xs<br>(but not both) are infinite. Oh, and "ns" are required to be ordered
<br>and non-negative.</blockquote><div><br>Nifty function there. =) And for the record, it works just fine if both lists are infinite -- it just produces an infinite output list, but it's lazy so no problem:<br><br>*Main> take 10 $ extract [1,3..] [2..]
<br>[3,5,7,9,11,13,15,17,19,21]<br><br><br>-Brent</div></div>