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