Scoping question


#1

I’m confused about how scope works in ulisp, or at least what’s valid or if I’m encountering bugs. Lets say I do

(defvar a 1)
(defvar b 2)

(defun add () (+ a b))

(defun test-add () 
   (let ((a 3) 
         (b 4)) 
        (add)))

(test-add)

Should this output 3, 7, or error?

What about

(defun add () (+ a b))

(defun test-add () 
   (let ((a 3) 
         (b 4)) 
        (add)))

(test-add)

and

(defvar a 1)
(defvar b 2)

(defun test-add () 
   (let* ((a 3) 
          (b 4)
          (add (lambda () (+ a b)))) 
         (add)))

(test-add)

#2

I’ve rewritten your examples slightly to make them distinct:

(defvar a 1)
(defvar b 2)

(defun add () (+ a b))

(defun test-add () 
   (let ((a 3) 
         (b 4)) 
        (add)))

(test-add)

This returns 7 in uLisp and Common Lisp because, although a and b have been defined as special (global) variables, (add) is evaluated in a context where they are bound to 3 and 4.

(defun add () (+ c d))

(defun test-add2 () 
   (let ((c 3) 
         (d 4)) 
        (add)))

(test-add2)

This gives errors in Common Lisp because c and d are undefined in the definition of add. It returns 7 in uLisp for the same reason as the first example.

(defvar e 1)
(defvar f 2)

(defun test-add3 () 
   (let* ((e 3) 
          (f 4)
          (add #'(lambda () (+ e f))))
         (funcall add)))

(test-add3)

This example has to be rewritten slightly for Common Lisp; it returns 7 in Common Lisp and uLisp for the same reason as the first example.