unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: Juri Linkov <juri@jurta.org>
Cc: Stefan Monnier <monnier@IRO.UMontreal.CA>, emacs-devel@gnu.org
Subject: power set (Re: Modified keypad keys)
Date: Mon, 01 Oct 2012 10:02:31 +0200	[thread overview]
Message-ID: <87lifqei20.fsf@rosalinde.fritz.box> (raw)
In-Reply-To: 87obkpypdk.fsf@mail.jurta.org

On Sat, 29 Sep 2012 22:19:35 +0300 Juri Linkov <juri@jurta.org> wrote:

>>> +(defun powerset (list)
>>> +  (if (null list)
>>> +      '(nil)
>>> +    (let ((ps (powerset (cdr list))))
>>> +      (append ps (mapcar (lambda (e) (cons (car list) e)) ps)))))
>>
>> I'm not sure I want `powerset' without some "<prefix>-".
>
> Maybe `cl-powerset', to be added to cl-seq.el.

I would like to have a power set function in Emacs. But why do you use
'(nil) for the empty set instead of '() or nil?  I use nil in a variant
of the above in a program I'm working on.  As an alternative to the
recursive definition, I've also been using this iterative bitwise
version (which I translated into Elisp from the C program at
http://rosettacode.org/wiki/Power_set#C):

(defun powerset-bitwise (l)
  (let ((binnum (lsh 1 (length l)))
	 pset elt)
    (dotimes (i binnum)
      (let ((bits i)
	    (ll l))
	(while (not (zerop bits))
	  (let ((arg (pop ll)))
	    (unless (zerop (logand bits 1))
	      (setq elt (append elt (list arg))))
	    (setq bits (lsh bits -1))))
	(setq pset (append pset (list elt)))
	(setq elt nil)))
    pset))

In my use case the cardinality of the power set is small and there's no
noticeable difference in speed between the recursive and the iterative
versions, but maybe it makes a difference for large sets.

Steve Berman



  parent reply	other threads:[~2012-10-01  8:02 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-28  6:40 Modified keypad keys Per Starbäck
2012-09-28  8:23 ` Stefan Monnier
2012-09-28 15:29   ` Juri Linkov
2012-09-28 20:16     ` Stefan Monnier
2012-09-29  2:14       ` Stefan Monnier
2012-09-29 19:19       ` Juri Linkov
2012-09-30  1:41         ` Stefan Monnier
2012-09-30  9:56           ` Juri Linkov
2012-09-30 19:45             ` Stefan Monnier
2012-10-01  9:29               ` Juri Linkov
2012-10-01 15:03                 ` Stefan Monnier
2012-10-04 18:41                   ` Juri Linkov
2012-10-04 23:26                     ` Andreas Schwab
2012-10-05 14:41                       ` Andreas Schwab
2012-10-05 16:43                         ` Juri Linkov
2012-10-05 16:55                           ` Andreas Schwab
2012-10-05 17:05                             ` Juri Linkov
2012-10-01  8:02         ` Stephen Berman [this message]
2012-10-01  8:35           ` power set (Re: Modified keypad keys) Christopher Monsanto
2012-10-01  9:07             ` Stephen Berman
2012-10-01 12:42               ` Lars Magne Ingebrigtsen
2012-10-01 13:06                 ` Stephen Berman
2012-10-01  9:27           ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lifqei20.fsf@rosalinde.fritz.box \
    --to=stephen.berman@gmx.net \
    --cc=emacs-devel@gnu.org \
    --cc=juri@jurta.org \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).