common lisp - Lexical versus dynamic scope, let form, declare form -


dynamic versus lexical scope.

; 1 expects lexical scope.     (let (( '(a)))    (defun func1 (x)     (setq (cons x (cons (func2 x) a))))   (defun func2 (y)      (setq (cons y a))))  (func1 'b) => (b (b a) b a) 

doing lexically, 1 expect following.

  1. substitute (a) a in func2.
  2. func2 called x, i.e. value b.
  3. func2 attaches value of a (a).
  4. so (cons y a) evaluates (b a).
  5. (setq (cons y a))) (b a).
  6. so func1 cons (b a) (a).
  7. x consed (b (b a) a)).
  8. end result (b (b a) a)? contradiction?

let try dynamical version. ; dynamic scope

(defparameter '(a))   (defun func1 (x)   (setq (cons x (cons (func2 x) a))))  (defun func2 (y)    (setq (cons y a)))  (func1 'b) =>(b (b a) b a) 

this works expected same in lexical scope?

but following not accepted @ all.

; won't work. (let (( '(a)))    (defun func1 (x)     (declare (special a))      (setq (cons x (cons (func2 x) a))))   (defun func2 (y)      (declare (special a))     (setq (cons y a))))  (func1 'b) error: attempt take value of unbound variable `a'.    

what going on?

thanks keen eyes. example 31 years old printed lisp textbook straight out of chapter "dynamic versus lexical scoping". explanation comes straight out of book. guess lexical scoping not checked, because authors explicitly warn readers lexical scoping not done in lisp. happy solved. stared time on it, without understanding going on. seemed odd contradiction.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -