Question: Behaviour of defparameter used with list

Question

Behaviour of defparameter used with list

Answers 0
Added at 2016-12-19 01:12
Tags
Question

So I encountered a rather strange behaviour that I couldn't explain/understand with what I googled so far nor the Gigamonkeys Book.

I tracked my problem down to the following minimal programm:

(defparameter *p1* '(0 0))

(defun f2 ()
  (setf (car *p1*) (+ (car *p1*) (cadr *p1*))
        (cadr *p1*) (+ (cadr *p1*) 1)))

(defun f1 ()
  (let ((*p1* '(0 1)))
    (format t "*p1* = ~a~%" *p1*)
    (f2)))

(f1) ;=> *p1* = (0 1)
(f1) ;=> *p1* = (1 2)
(f1) ;=> *p1* = (3 3)
(f1) ;=> *p1* = (6 4)
(format t "*p1* = ~a~%" *p1*)

So as you can see the snippet defines a parameter and 2 nested functions. The toplevel function f1 creates a binding via let and calls the inner function f2, which just does some arbitrary arithmetic and assigns new values to global parameter's elements.

So my question is: Why does the output change from function call to function call? I have no idea what causes this behaviour. I'm probably overlooking something obvious right now, but please could someone point out what this is?

Or is it just that one shouldn't use lists for dynamically scoped variables?

Answers to

Behaviour of defparameter used with list

Source Show
◀ Wstecz