<div>What about something like:</div><div><br></div><div>combinations n = filter (\s -&gt; length s == n) . subsequences</div><div><br></div><div>hth,</div><div>L.</div><div><br></div><div><br></div><br><div class="gmail_quote">
On Sun, Nov 13, 2011 at 10:42 PM, Peter Hall <span dir="ltr">&lt;<a href="mailto:peter.hall@memorphic.com">peter.hall@memorphic.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi all,<br>
This function took me a long time to write, getting my head around the<br>
double recursion. It returns a list of all possible sub-sets of a<br>
given length from a list.<br>
<br>
I have a couple of questions.<br>
<br>
1. Can it be improved for efficiency or style?<br>
2. Is there a library that already has this and other related<br>
functions? I assumed there would be but I couldn&#39;t find it on hoogle.<br>
<br>
<br>
combinations :: Int -&gt; [a] -&gt; [[a]]<br>
combinations _ [] = []<br>
combinations 0 _ = []<br>
combinations 1 x = map (:[]) x<br>
combinations n (x:xs) = (map (x:) $ combinations (n-1) xs) ++ combinations n xs<br>
<br>
e.g.<br>
&gt; combinations 3 [1..5]<br>
[[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]<br>
<br>
Thanks,<br>
Peter<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>