This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Help on setf behavior

3 posts · 2813 views

HI
I do not understand why this expr :
(setf lst nil)
(setf (car lst)(1+ (car (push 1 lst))))

2

lst
(1 2)

return 2 it’s ok, but now if we call lst return is : (1 2)-…??
Instead I’ll expect ( 2 1)…
can somebody
illuminate me?? thanks in advance

Re: Help on setf behavior

Because push expands roughly (see notes on CLHS) to:
(setf place (cons item place))
so your code looks like this:
(defvar lst '(nil))
(setf (car lst) (1+ (car (setf lst (cons 1 lst)))))
The place of first setf is evaluated before the value form which has a side effect that the car of lst becomes the cdr of lst, due to consing.
// I've used defvar and a non-empty list, otherwise SBCL would be complaining.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Help on setf behavior

ooohh PERFECT!!
Thanks Goheeca !!! very much!! :D