Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:50:12+00:00 2026-05-27T09:50:12+00:00

disclaimer: I’m pretty sure I’ve managed to muck up something really simple, possibly because

  • 0

disclaimer: I’m pretty sure I’ve managed to muck up something really simple, possibly because I’ve been poking at this in between
“real work” while waiting for some slow C++ builds, so my head’s not
in the right place.

In looking at
What's the most efficient way of generating all possible combinations of skyrim (PC Game) potions? I had the naïve notion that it would be a really, really simple recursive filter in Lisp to generate all combinations of size “n.” The answer given there, in R, is elegant and shows off the language well, but that combn(list,n) method caught my attention. ( http://stat.ethz.ch/R-manual/R-patched/library/utils/html/combn.html )

(defun combn (list n)
  (cond ((= n 0) nil)
        ((null list) nil)
        ((= n 1) (mapcar #'list list))
        (t (mapcar #'(lambda (subset) (cons (car list) subset))
                   (combn (cdr list) (1- n))))))

(combn '(1 2 3 4 5 6 7 8 9) 3)

((1 2 3) (1 2 4) (1 2 5) (1 2 6) (1 2 7) (1 2 8) (1 2 9))

Except, this just returns the first set of combinations … I can’t wrap my head around what’s wrong, precisely. It seems that the (= n 1) case works right, but the t case should be doing something differently, such as stripping (1 2) off the list and repeating?

So, my attempt to fix it, got nastier:

 (defun combn (list n)
  (cond ((= n 0) nil) ((= n 1) (mapcar #'list list))
        ((null list) nil)
        (t (cons (mapcar #'(lambda (subset) (cons (car list) subset))
                         (combn (cdr list) (1- n)))
                 (combn (cdr list) n)))))

which is wrong at the point of (t cons(… I think. But, if cons is the wrong answer, I’m not sure what is right…? (Reduced to using 2 to demonstrate output…)

(combn '(1 2 3 4 5 6 7 8 9) 2)
(((1 2) (1 3) (1 4) (1 5) (1 6) (1 7) (1 8) (1 9))
 ((2 3) (2 4) (2 5) (2 6) (2 7) (2 8) (2 9))
 ((3 4) (3 5) (3 6) (3 7) (3 8) (3 9))
 ((4 5) (4 6) (4 7) (4 8) (4 9))
 ((5 6) (5 7) (5 8) (5 9))
 ((6 7) (6 8) (6 9))
 ((7 8) (7 9))
 ((8 9))
  NIL)

… which appears to be right, except for the extraneous nesting and the bonus NIL at the end. (I had anticipated that ((null list) nil) would have filtered that out?)

What did I do wrong? 🙁

(And, also, is there a standard routine for doing this more efficiently?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:50:12+00:00Added an answer on May 27, 2026 at 9:50 am

    Yes, the cons is not the right thing, you need an append. And that’s also what gets you the NIL at the end. I can’t write Lisp, so I’ll give you Haskell:

    comb :: Int -> [a] -> [[a]]
    comb 0 _ = [[]]
    comb k (x:xs) = [x:ys | ys <- comb (k-1) xs] ++ comb k xs
    comb _ _ = []
    

    That’s short and sweet, but inefficient (and doesn’t check for negative k). It will often try to choose more elements than the list has for a long time. To prevent that, one would keep track of how many elements are still available.

    comb :: Int -> [a] -> [[a]]
    comb 0 _ = [[]]
    comb k xs
      | k < 0     = []
      | k > len   = []
      | k == len  = [xs]
      | otherwise = go len k xs
        where
          len = length xs
          go l j ys
            | j == 1 = map (:[]) ys
            | l == j = [ys]
            | otherwise = case ys of
                            (z:zs) -> [z:ws | ws <- go (l-1) (j-1) zs] ++ go (l-1) j zs
    

    Ugly, but efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I'm sure this has been answered before but I cannot find anything on
Disclaimer: I know this type of question has been asked here before, I just
Disclaimer: I'm pretty terrible with multithreading, so it's entirely possible I'm doing something wrong.
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: This is for a homework assignment, but the question is not regarding the
DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer: I realize I can generate this at runtime in Java, this was needed
Disclaimer : I realize asking Why doesn't my regular expression work is pretty amateur.
Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.