53 lines
1.5 KiB
HTML
53 lines
1.5 KiB
HTML
#! /usr/bin/gcl -f
|
|
|
|
; (eval '(expression))
|
|
; --------------------
|
|
; given a lisp expression as a list, eval can be
|
|
; used to evaluate it
|
|
|
|
; run eval on the list (+ 1 2 3 4)
|
|
(setf x (eval '(+ 1 2 3 4)))
|
|
|
|
; consider what happens if we run the following:
|
|
(setf x (eval (+ 1 2 3 4))) ; lisp evaluates the (+ 1 2 3 4) first,
|
|
; *then* runs (eval 10)
|
|
|
|
; examples of eval and quoting
|
|
(defvar x 1)
|
|
(eval x) ; 1 - lisp evaluates x as 1, then runs (eval 1)
|
|
(eval 'x) ; 1 - eval is looking up the value associated with symbol x
|
|
(eval (quote x)) ; 1 - as above
|
|
(eval '(quote x)) ; x
|
|
|
|
; print the result
|
|
(format t "~A~%" x)
|
|
|
|
; build an expression using cons then pass it to eval and print the result:
|
|
(format t "~A~%" (eval (cons '* '(10 20))))
|
|
|
|
; function that accepts an expression as a parameter and runs eval on it
|
|
(defun evalon (expr)
|
|
(eval expr))
|
|
|
|
(format t "result of (evalon '(sqrt 4)) is ~A~%" (evalon '(sqrt 4)))
|
|
|
|
; function that takes a list of expressions, L, as a parameter
|
|
; and runs eval on each of them, in order
|
|
(defun evalEach (L)
|
|
(cond
|
|
((not (listp L)) L)
|
|
((null L) nil)
|
|
(t (block
|
|
evalBlock
|
|
(eval (car L))
|
|
(evalEach (cdr L))))))
|
|
|
|
(evalEach '((format t "this is expression 1~%")
|
|
(format t "this is expression 2~%")))
|
|
|
|
; you can also specify the conditions under which eval will be run:
|
|
; - at compile time
|
|
; - at load time
|
|
; - at time of interpretation
|
|
(eval when (compile load eval) (format t "hi!"))
|
|
|