On Friday, November 9, 2012 11:40:06 AM UTC-5, Barry Margolin wrote:
> You're making a silly mistake, see below.
Yes, thanks, I see that.
> The syntax of SETF is:
> (setf <place> <new-value>)
> Compare that to what you wrote. Fix the discrepancy and your function
> will work.
>
> There's one other thing: you shouldn't perform destructive modification
> of a data structure created using a literal (i.e. quoted) expression.
> So change your DEFPARAMETER to use functions to create fresh object, i.e.
> (defparameter list-1 (list (cons 0 2) (cons 1 3) ...))
I'm trying my hand at a mancala type game (by way of explanation). Here is what I finally came up with. I won't ask you for comments, but would appreciate it if you have any.
-------------code--------------------
(defun drop-pebbles (state square)
(let* ((factor (length state)) ;this is the factor for the modulus operation
(target (car (subseq state square (1+ square)))) ;finds the target element for further processing
(num-pebbles (cdr target)) ; get the number of pebbles to drop
(start-square (mod (1+ square) factor))
(new-state (copy-alist state)))
(setf (cdr (nth square new-state)) 0)
(drop-pebbles-helper new-state num-pebbles start-square factor)))
(defun drop-pebbles-helper (new-state num-pebbles start-square factor)
(do ((pebbles num-pebbles (1- pebbles))
(element start-square (1+ element)))
((zerop pebbles) new-state)
(progn
(incf (cdr (nth (mod element factor) new-state))))))
----------------run twice-------------------
* list-1
((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1))
* (drop-pebbles list-1 2)
0: (DROP-PEBBLES ((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1)) 2)
1: (DROP-PEBBLES-HELPER ((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1)) 0
3 6)
1: DROP-PEBBLES-HELPER returned
((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1))
0: DROP-PEBBLES returned ((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1))
((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1))
* (drop-pebbles list-1 5)
0: (DROP-PEBBLES ((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1)) 5)
1: (DROP-PEBBLES-HELPER ((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 0)) 1
0 6)
1: DROP-PEBBLES-HELPER returned
((0 . 3) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 0))
0: DROP-PEBBLES returned ((0 . 3) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 0))
((0 . 3) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 0))
*