If I have a associate list ((:a a) (:b b) (:c c)), how can I set b to (:b z)?
Can you tell me different ways to do it?
Thanks!!
joybee wrote:If I have a associate list ((:a a) (:b b) (:c c)), how can I set b to (:b z)?
Can you tell me different ways to do it?
Thanks!!
(let ((lst '((:a a) (:b b) (:c c))))
(setf (car (cdr (car (cdr lst)))) 'z) ;abra-cadadr!
lst)
(let ((lst (list '(a a) '(b b) '(c c))))
(rplacd (assoc 'b lst) 'z))
(car (cdr (car (cdr <list>)))) => (cadadr <list>)(rplacd (assoc <key> <a-list>) <new-value>)Duke wrote:Yet the list form and the quoted form evaluate to the same thing.
Ramarren wrote:Not quite. LIST is a function which creates a list at runtime, whereas QUOTE is a special operator which creates a list at compile time. Quoted forms are treated as source literals, and modifying them has an unspecified behaviour, which in principle might include a memory protection fault if they are placed in read-only memory. I don't think any implementation actually does that, but modifying literals is usually not a good idea.
joybee wrote:Thanks, everyone!
Here is a little example I ran on sbcl.
* (equal (list 1 2) (rplacd (list 1 1) 2))
* NIL
* (rplacd (list 1 1) 2)
* (1 . 2)
What I want is after value reset the evaluation of equal returns T. How can I achive that?
(rplacd (list 1 1) 2) => (1 . 2)
(list 1 2) => (1 . (2 . nil))
(equal (cons 1 (cons 2 nil)) (list 1 2)) => T
(equal (cons 1 2) (rplacd (list 1 1) 2)) => T
Duke wrote:Ramarren wrote:Not quite. LIST is a function which creates a list at runtime, whereas QUOTE is a special operator which creates a list at compile time. Quoted forms are treated as source literals, and modifying them has an unspecified behaviour, which in principle might include a memory protection fault if they are placed in read-only memory. I don't think any implementation actually does that, but modifying literals is usually not a good idea.
I see. I guess I should have read the documentation on QUOTE while I was at it.
Thanks.joybee wrote:Thanks, everyone!
Here is a little example I ran on sbcl.
* (equal (list 1 2) (rplacd (list 1 1) 2))
* NIL
* (rplacd (list 1 1) 2)
* (1 . 2)
What I want is after value reset the evaluation of equal returns T. How can I achive that?
(rplacd (list 1 1) 2) => (1 . 2)
(list 1 2) => (1 . (2 . nil))
(cons 1 (cons 2 nil))(equal (cons 1 2) (rplacd (list 1 1) 2)) => T((:a . a) (:b . b) (:c . c))(let ((lst (list (list :a 'a) (list :b 'b) (list :c 'c))))
(setf (second (assoc :b lst)) 'z)
lst)
=> ((:A A) (:B Z) (:C C))(list (cons :a 'a) (cons :b 'b) (cons :c 'c))
=> ((:A . A) (:B . B) (:C . C))(let ((lst (list (cons :a 'a) (cons :b 'b) (cons :c 'c))))
(rplacd (assoc :b lst) 'z)
lst)
=> ((:A . A) (:B . Z) (:C . C))
(equal (cons 1 2) (rplacd (cons 1 1) 2))
=> T
(rplacd (cons 1 1) 2)
=> (1 . 2)Ramarren wrote:Duke wrote:Yet the list form and the quoted form evaluate to the same thing.
Not quite. LIST is a function which creates a list at runtime, whereas QUOTE is a special operator which creates a list at compile time. Quoted forms are treated as source literals, and modifying them has an unspecified behaviour, which in principle might include a memory protection fault if they are placed in read-only memory. I don't think any implementation actually does that, but modifying literals is usually not a good idea.
CL-USER> (defun literal ()
(quote (1 2 3)))
LITERAL
CL-USER> (defun constructed ()
(list 1 2 3))
CONSTRUCTED
CL-USER> (eq (literal) (literal))
T
CL-USER> (eq (constructed) (constructed))
NIL
CL-USER> (setf (cadr (literal)) 50)
50
CL-USER> (literal)
(1 50 3)
CL-USER> (setf (cadr (constructed)) 50)
50
CL-USER> (constructed)
(1 2 3)
Users browsing this forum: No registered users and 0 guests