Is SBCL for lisp handling scope differently? Doesn't it seem to cover the scope of the called functions?

When using emacs or my Android app, I run

(defun big (num) (setf num2 5)(little num)))
(defun little (num)(+ num2 num))

Little happily accepts num2, but when I run it in my SBCL replica (with sublimetext3), it is not.

It is right?

What is a workaround without creating a global variable for num2?

I could just pass the second argument (little num num2)

But this does not work when I try to display a map Littleabove the list. Because I can have only one argument, if the correct map?

+4
source share
1 answer

Please read §6. Variables from Practical General Lisp .

Emacs Lisp, Common Lisp (Emacs Lisp ). (.. ) , , , ( "" ), *standard-output*. defparameter defvar, . , ; , setf Common Lisp: num2; , , / - .

, , : , let:

(let ((*standard-output* *error-output*))
  (print "Stream redirection"))

print , *standard-output*; , *error-output*. let, *standard-output* (, ).

, , ( ), : ( ) let:

(let ((closure
        (let ((count 0))
          (lambda () (print (incf count))))))
  (funcall closure)
  (funcall closure))

;; prints:
;; 1
;; 2

lambda , count. , , count . , .

MAPCAR

, ?

; , mapcar, , , ( ):

(mapcar (lambda (x y) (* x y))
        '(1 2 3)
        '(0 3 6))
=> (0 6 18)

(mapcar #'list '(1 2) '(a b) '(+ /))
=> ((1 a +) (2 b /))

.

...

(defun adder (x)
  (lambda (y) (+ x y)))

(mapcar (adder 10) '(0 1 2))

=> (10 11 12)

adder x , y (+ x y).

...

, :

(defparameter *default-offset* 0)

... :

(defun offset (x)
  (+ x *default-offset*))

mapcar:

(let ((*default-offset* 20))
  (mapcar #'offset '(1 2 3)))

=> (21 22 23)

jkiiski , (declare (special ...)), ( let, a defun,...). progv. "" , . .

+10

Source: https://habr.com/ru/post/1692580/


All Articles