Commits

Anonymous committed 390911a

adde nchoose k function

Comments (0)

Files changed (1)

   res
     
 
+let rec ht_fold f acc xs = 
+    match xs with
+    |[] -> acc
+    |b::bs  -> ht_fold f (List.rev_append (f b bs) acc) bs
 
+let rec nchoosek ns = 
+  function
+    |0 -> [[]]
+    |k -> let f x xs = 
+		List.fold_left (fun a b -> (x::b)::a) [] (nchoosek xs (k-1))
+	  in ht_fold f [] ns 
+	  
 
-let  n_choose_k n k = 
-  let rec choose lo = 
-    function
-      |[] -> [[]]
-      |x::xs as cs -> if lo = 1 then List.map (fun a -> [a]) cs
-	              else 
-	                List.map (fun cs -> x::cs) (choose (lo-1) xs)
-			  
-  in
-  choose k n