Variables & Side Effects

June 8, 2008 | Filed Under Common Lisp - Touretzky 

———————————————————
Chapter 5
———————————————————

SETF assigns a global variable:
(SETF n 1)
(SETF x ‘(1 2 3))
(LENGTH x) -> 3
(REST x) -> (2 3)

variables have scope - to create a local variable use LET:

(DEFUN MANIP (op x y)
(LET ( (SUM (+ x y)) (PROD (* x y)) )
(COND ((EQUAL op ‘AVG) (/ SUM 2.0))
((EQUAL op ‘MULT) PROD))))

Form is (LET (define-vars) (function using vars) )

To use the results of one var immediately within the define-vars, use LET*

(DEFUN MANIP2 (op x y)
(LET* ( (SUM (+ x y)) (SUMSQ (* SUM SUM)) )
…..

Documentation:

(DEFUN MY-TEST-FUNCTION (x)
“this is my test function”
((+ x 2)))

(DOCUMENTATION ‘my-test-function ‘function) -> “this is my test function”

;;; marks a comment outside of a function
;; marks a comment inside a function on its own line
; marks a comment inside a function alongside some code

The second pointer in a symbol block points to the value of the symbol.

A symbol may have a value and a separate function - i.e. we may define CAR as equal to ROLLS-ROYCE, but the function CAR will be unaffected and the compiler figures out from context which CAR is being referred to.

Comments

Leave a Reply