Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion destructively changing the cdr of a sublist in a function
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Pascal J. Bourguignon  
View profile  
 More options Nov 9 2012, 6:40 pm
Newsgroups: comp.lang.lisp
From: "Pascal J. Bourguignon" <p...@informatimago.com>
Date: Sat, 10 Nov 2012 00:40:41 +0100
Local: Fri, Nov 9 2012 6:40 pm
Subject: Re: destructively changing the cdr of a sublist in a function

ccc31807 <carte...@gmail.com> writes:
> 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))

Try:

(defun display-mancala (state &optional (stream *standard-output*))
  (let* ((n/2 (truncate (length state) 2))
         (above (subseq state 0 n/2))
         (below (reverse (subseq state n/2))))
    (format stream
            "~{+~*-----~}+~%~:*~{|~3D  ~}|~%~{+~*-----~}+~%~:*~{|~3D  ~}|~%~:*~{+~*-----~}+~%"
            (mapcar (function cdr) above)
            (mapcar (function cdr) below))))

(display-mancala '((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1)))
+-----+-----+-----+
|  2  |  1  |  0  |
+-----+-----+-----+
|  1  |  3  |  0  |
+-----+-----+-----+

If you encapsulate your lists in clos objects, then you can write a
print-object method so that the debugger prints this ASCII-art instead
of the a-lists:

(defclass mancala ()
  ((state :type list :initarg :state :accessor mancala-state)))

(defmethod print-object ((self mancala) stream)
  (if *print-readably*
      (print-unreadable-object (self stream))
      (display-mancala (mancala-state self) stream))
  self)

(make-instance 'mancala :state '((0 . 2) (1 . 1) (2 . 0) (3 . 0) (4 . 3) (5 . 1)))
+-----+-----+-----+
|  2  |  1  |  0  |
+-----+-----+-----+
|  1  |  3  |  0  |
+-----+-----+-----+

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.