Архив рубрики: Common Lisp

Common Lisp: динамические (специальные) переменные

Вместо тысячи слов:

package main
 
import (
	"log"
)
 
var global = 1
 
func main() {
        log.Println("Global before", global)
        func() {
                global := 2
                log.Println("Global in closure", global)
                test()
        }()
        log.Println("Global after", global)
}
 
func test() {
        log.Println("Global in test", global)
}
 
// Global before 1
// Global in closure 2
// Global in test 1
// Global after 1
(defparameter *global* 1)
 
(defun main ()
  (format t "Global before ~a~%" *global*)
  (funcall (lambda ()
             (let ((*global* 2))
               (format t "Global in closure ~a~%" *global*)
               (test))))
  (format t "Global after ~a~%" *global*))
 
(defun test ()
  (format t "Global in test ~a~%" *global*))
 
(main)
 
;; Global before 1
;; Global in closure 2
;; Global in test 2
;; Global after 1