lisp声明、使用变量的一种方法,是使用let
语句。
形如:
1
2
3
4
5
6
7
8
9
10
11
12
| ;(let ((variable declare1) (variable declare2) (...))
; (varaible used here));
(defun foo(x)
(format t "Parameter: ~a~%" x)
(let ((x 2))
(format t "Outer LET: ~a~%" x)
(let ((x 3))
(format t "Inner LET: ~a~%" x))
(format t "Outer LET: ~a~%" x))
(format t "Parameter: ~a~%" x));
(foo 10);
|
声明的作用域,和C语言很相似,存在覆盖的特点。输出:
1
2
3
4
5
| Parameter: 10
Outer LET: 2
Inner LET: 3
Outer LET: 2
Parameter: 10
|
使用let
声明时,变量声明域内,无法使用前一个在本声明域内声明的变量:
1
2
3
4
5
6
| (defun year-day(y)
(let ((m (* y 12)) (d (* m 30)))
(format t "Year:~d~%Month:~d~%Day:~d~%" y m d)));
(year-day 1);
*** - LET: variable M has no value
|
使用let*
可以进行如此操作:
1
2
3
4
5
6
7
| (defun year-day(y)
(let* ((m (* y 12)) (d (* m 30)))
(format t "Year:~d~%Month:~d~%Day:~d~%" y m d)));
(year-day 1);
Year:1
Month:12
Day:360
|
just like this.