(push-output 'mytest.out) ; Lisp test file ; Don Hopkins ; (function fn) returns a funarg pair, of the form (fn . env), where ; fn is a lisp function that can be applied, and env is the ; environment in which (function fn) was evaluated. When the funarg ; pair is given as a function to apply or eval, the function will be ; evaluated in the environment env. ; Initialize foo and bar. (setq foo 'godzilla bar 'moon-moth) ; Define a function that references the free variable foo. (defun with-foo (x) (list foo 'is 'with x)) ; Create an environment in which foo and bar are rebound, and with-foo ; is rebound to another function that also references the free ; variable foo. (let ((foo '(turkey lurkey)) (bar '(the banana bunch)) (with-foo (lambda (other-x) (list foo 'is 'in 'this 'case 'actually 'with other-x)))) ; In this environment, bind bletch to functions that will be evaluated ; in this environment when applied. (setq bletch (function (lambda (fn) (fn bar))))) ; Call bletch, giving it the atom with-foo. The atom with-foo is ; evaluated in bletch's environment, so the local definition of ; with-foo is called. The bar in bletch and the foo in the local ; with-foo are all gotten from bletch's environment, (bletch 'with-foo) ; Call bletch, giving it the funarg with-foo. The atom with-foo is ; evaluated in the environment (function with-foo) was evaluated in, ; so the global definition of with-foo is called. The bar in bletch is ; gotten from bletch's environment, because bletch is a funarg. The ; foo in the global with-foo is gotten from the environment that ; (function with-foo) was evaluated in. (bletch (function with-foo)) ; The argument field (the car) of a user function, user special, or ; macro consists of either a list of atoms that the arguments are ; bound to, or an atom that the entire argument list is bound to. If ; the arg it is a list ending with a dotted atom, then all the rest of ; the arguments at that point are bound to the atom. (defspecial atom-arg foo (printcr foo)) (atom-arg can take any number of arguments, the list of which is bound to the atom.) (defspecial list-arg (foo bar baz) (printcr foo) (printcr bar) (printcr baz)) (list-arg takes three parameters.) (defspecial list-dot-arg (foo bar baz . bletch) (printcr foo) (printcr bar) (printcr baz) (printcr bletch)) (list-dot-arg takes three args and as many more as you care to give it.) (list-dot-arg also takes three) ; Demonstrations of various lisp functions. (plus 1000 200 30 4 .5) (times 2 3 4 .5) (quotient 1 2) (remainder 10 4) (minus 20) (floor 4.4) (floor -4.3) (numberp t) (numberp -54.5) (greaterp -23 332) (zerop 0) (zerop -2) (difference 3 90) (lessp 3 .032) (into minus '(1000 200 30 4)) (onto printcr '(1000 200 30 4)) (set 'foo 'bar) (set foo 'baz) bar (null '(foo bar)) (null 12) (null nil) (not (null t)) (not (null nil)) (consp 'foo) (consp list) (consp consp) (consp '(foo bar baz)) (consp '(1 . 3)) (eq '(foo (bar (baz))()((()))) '(foo (bar (baz))()((())))) (equal '(foo (bar (baz))()((()))) '(foo (bar (baz))()((())))) (memq 'is '(what is the meaning of life?)) (memq '(grape jam) '(toast (orange juice) (grape jam))) (member 'is '(what is the meaning of life?)) (member '(grape jam) '(toast (orange juice) (grape jam))) (append '(a b c) '(1 2 3)) (reverse '(10000 1000 100 10 1)) (reverse '(m a d a m i m a d a m)) (setq red '(seems that red was evaluated) green '(looks like green was evaluated) blue '(i would say blue was evaluated)) (body (bind (lambda (green) (cons red (list (cons -32 green) blue))))) (let ((green 'bright) (red 'dull) blue) (print (list red green blue))) (pop-output)