It has been a long time that I haven't dealt with my blog. On one hand I was preparing the exams.On the other hand I just learn more things and take some documents at local PC. As a result, I begin to learn Lisp as planned.
2.10 Variables
let ; define local variables
; x y are local variables
(let ((x 1) (y 2))
(+ x y)
)
; the function doesn't stop until you enter a number
(defun ask-number ()
(format t "Please enter a number. ")
(let ((val (read)))
(if (numberp val)
val
(ask-number)
)
)
)
defparameter ; define global variables
; to avoid the same with local variables, we use **
> (defparameter *glob* 99)
defconstant ; define constants
> (defconstant limit (+ *glob* 1))
boundp ; test whether a symbol is a global variable or constant
> (boundp '*glob*)
2.11 Assignment
setf ; assign global/local variables
(setf *glob* 98)
; if the first variable is a symbol but not a name of local variable, setf will set it as global variable
(let ((n 10))
(setf n 2)
n
)
setf can also:
(defparameter x (list 'a 'b 'c))
(setf (car x) 'n)
; x will be (N B C)
setf can also:
(setf a 'b c 'd e 'f)
2.12 Function Programming
Function Programming means using the return value and not changing original things. It allows interactive testing
; lst won't be changed
(setf lst '(c a r a t))
(remove 'a lst)
; x will be changed
(setf x (remove 'a x))
; so you'd better not use functions like setf
2.13 Iteration
do is the basic iterative symbol and it can create variables. The first actual variable is a format list of a group variables; each element in the list is (variable initial update)
The second actual variable contains one or more expressions.The first expression used to test whether the iteration reach end.The rest expressions will be calculated and the last expression's value will be returned
; iteration
(defun show-squares (start end)
(do ((i start (+ i 1))) ; counter
((> i end) 'done) ; done will be returned
(format t "~A ~A~%" i (* i i)) ; main iteration
)
)
; recursive
(defun show-squares (start end)
(if (> start end)
'done
(progn ; accept expressions and return the last value
(format t "~A ~A~%" start (* start start))
(show-squares (+ start 1) end)
)
)
)
; iterate in a list:
(defun our-length (lst)
(let ((len 0))
(dolist (obj lst)
(setf len (+ len 1))
)
len
)
)
; recursive version
(defun our-length (lst)
(if (null lst)
0
(+ (our-length (cdr lst)) 1)
)
)
; it is not tail-recursive