Results 1 to 1 of 1

Thread: relation between power set and binary

  1. #1
    Join Date
    Jan 2006
    Beans
    961

    Smile relation between power set and binary

    Code:
    (defmethod power-set-of ((list list))
      (let ((sets nil) (list-length (length list)))
        (dotimes (counter (expt 2 list-length))
          (let ((set nil))
            (dotimes (index list-length)
              (when (logbitp index counter)
                (push (nth index list) set)))
            (push set sets)))
        sets))
    recursion/iteration might have turned out better time/space-wise, but this is a fun way of doing it i think

    (`loop' instead of `dotimes' would probably shorten the code quite a bit.. but generally i don't care; it's only for code-generating code with 6-8 elements anyways )

    how would you guys find a power set?
    Last edited by lnostdal; February 10th, 2007 at 01:49 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •