Skip to content

Instantly share code, notes, and snippets.

@cdosborn
Last active August 29, 2015 14:07
Show Gist options
  • Save cdosborn/4fc91f05f0810132a1ff to your computer and use it in GitHub Desktop.
Save cdosborn/4fc91f05f0810132a1ff to your computer and use it in GitHub Desktop.
Recursion Spider Weasel
choose :: [a] -> Int -> [[a]]
choose _ 0 = [[]]
choose [] k = []
choose (x:xs) k = thoseWith ++ thoseWithout
where
thoseWith = map (x :) (choose xs (k - 1))
thoseWithout = choose xs k
function combinations(numArr, choose, callback) {
var n = numArr.length;
var c = [];
var temp = [];
var inner = function(start, choose_) {
if (choose_ === 0) {
c.push(temp.slice());
} else {
for (var i = start; i <= n - choose_; i++) {
temp.push(numArr[i]);
inner(i + 1, choose_ - 1);
temp.pop();
}
}
}
inner(0, choose);
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment