Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!newsfeed.internetmci.com!howland.reston.ans.net!news.cac.psu.edu!news.math.psu.edu!chi-news.cic.net!newsspool.doit.wisc.edu!night.primate.wisc.edu!aplcenmp!hall
From: hall@aplcenmp.apl.jhu.edu (Marty Hall)
Subject: Re: permutation
Message-ID: <DMB35D.4u4@aplcenmp.apl.jhu.edu>
Organization: JHU/APL Research Center, Hopkins P/T CS Faculty
References: <4eodp9$gur@sungod.isi.edu> <DM3n2L.2rL@aplcenmp.apl.jhu.edu> <3115004B.512A@fst.se>
Date: Mon, 5 Feb 1996 14:05:36 GMT
Lines: 37

In article <3115004B.512A@fst.se> Peter Bengtson <joker@fst.se> writes:
>Marty Hall wrote:
>> PAIP has an 8-line solution on page 150.
[...]
>Thanks for this immensely useful information. I must say I'm thrilled. 
>All that remains is to figure out what PAIP is (a book?) then find a 
>bookshop carrying it, pay up, carry it home and turn to page 150. Maybe 
>I'll like the algorithm. Who says the Internet doesn't make life easier?
>
>Seriously, Marty, how about posting those 8 lines of code? I'd really 
>like to see them!

OK, mea culpa! In a weak attempt at defense, all I can say is that I
consider PAIP a standard reference like CLtL, but I suppose it was
obvious that the person asking the question wasn't familiar with it
(or they wouldn't have asked :-).

PAIP: _Paradigms of AI Programming_, Peter Norvig. I posted the URL
for info on the text and the source code last week. IMHO the
definitive text on AI applications in Common Lisp and advanced CL in
general (although Graham's _On Lisp_ has better coverage of macros).

Here's the code from pg 150, minus the comments and doc string:

(defun permutations (bag)
  (if (null bag)
      '(())
      (mapcan #'(lambda (e)
		  (mapcar #'(lambda (p) (cons e p))
			  (permutations
			    (remove e bag :count 1 :test #'eq))))
	      bag)))

Cheers-
					- Marty
(proclaim '(inline skates))   
http://www.apl.jhu.edu/~hall/lisp.html
