Re: combinations not working



"John Thingstad" <john.thingstad@xxxxxxxxx> writes:

This is a excerpt from my code:
[...]

Problem:
1. The combinations are not computed right.
To dumb to see what I am missing I guess.
ex: (print-combinations '(1 2 3))
1: (1 2 3)
2: (1 3 2)
3: (2 1 3)
4: (2 3 1)
5: (1 2 3)
6: (1 3 2)
What is wrong? Is there a better way?

You don't put the rotated elements back in place. The following work:

(defun permute (vector index action)
(cond
((zerop index) (funcall action vector))
(t (permute vector (1- index) action)
(dotimes (i index)
(rotatef (aref vector index) (aref vector i))
(permute vector (1- index) action)
(rotatef (aref vector index) (aref vector i))))))

C/USER[830]> (permute #(1 2 3) 2 (function print))

#(1 2 3)
#(2 1 3)
#(3 1 2)
#(1 3 2)
#(1 2 3)
#(2 1 3)
NIL

If you remove the last ROTATEF, it gives results similar to yours.


2. The macro output (with the ; removed) just prints the call verbatim.
ex. (output (nrotate copy start (1+ start)) prints
(nrotate copy start (1+ start))
I would like it to resolve the variables.
ex. (output (nrotate copy start (1+ start)) prints
(nrotate #(1 2 3) 1 2)
How do I do this?

(output `(nrotate ,copy ,start ,(1+ start)))

A better way is not to modify your source, and use TRACE (and UNTRACE
when you're done debugging).


--
__Pascal Bourguignon__ http://www.informatimago.com/

"Debugging? Klingons do not debug! Our software does not coddle the
weak."
.