Search Results
7/24/2025, 2:59:44 PM
>>106004188
>Can someone explain lexical scoping? I've implemented my own lisp, but it uses dynamic scope, and I don't quite understand how lexical scope works on the level of implementation.
;; a lambda like
(lambda (x y) x)
;; is represented internally as a closure (the parameter list, body list, and current environment)
(((x y) (x)) environment)
;; which is really (at toplevel, where the environment only contains a lambda primitive)
(((x y) (x)) ((lambda . #<procedure>)))
;; let's evaluate this application
((lambda (x y) x) 1 2)
;; during apply the current environment is extended using
(cons (map cons (x y) (1 2)) environment)
;; producing a new environment (if we had another closure in the body list, this is the environment that would get captured the same way as before)
(((x . 1) (y . 2))
((lambda . #<procedure>) ...))
;; the body list (x) is then evaluated in the new environment
(last (map eval (x) (((x . 1) (y . 2)) ((lambda . #<procedure>) ...))))
>Can someone explain lexical scoping? I've implemented my own lisp, but it uses dynamic scope, and I don't quite understand how lexical scope works on the level of implementation.
;; a lambda like
(lambda (x y) x)
;; is represented internally as a closure (the parameter list, body list, and current environment)
(((x y) (x)) environment)
;; which is really (at toplevel, where the environment only contains a lambda primitive)
(((x y) (x)) ((lambda . #<procedure>)))
;; let's evaluate this application
((lambda (x y) x) 1 2)
;; during apply the current environment is extended using
(cons (map cons (x y) (1 2)) environment)
;; producing a new environment (if we had another closure in the body list, this is the environment that would get captured the same way as before)
(((x . 1) (y . 2))
((lambda . #<procedure>) ...))
;; the body list (x) is then evaluated in the new environment
(last (map eval (x) (((x . 1) (y . 2)) ((lambda . #<procedure>) ...))))
Page 1