Special Declaration and Variables in Common Lisp

706 views Asked by At

I am reading the Common Lisp Hyperspec and am struggling to understand the concept behind the Special declaration.

Namely, what are the special variables and why would we want them? I gathered that somehow we can change which scope the variable belongs to in the eyes of the compiler by using the special declaration, but the details elude me.

Could someone explain and perhaps give some examples?

1

There are 1 answers

0
Sylwester On

Special variables are dynamically bound. That means:

(defvar x 10)

(defun test (v) 
  (+ v x)) 

(let ((x 20))
  (test x)) 
; ==> 40

Notice that lexical rules of scoping does not apply. Rather than that it behaves like the very first Lisp. If global variables would not been special the result would be 30. You can introduce hard to find bugs by accidentally declaring a variable special. To avoid this we use *earmuffs*.