ANSI Common Lisp Learn
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
最新文章
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(18)-权限管理系统-表数据
- visual studio 2013 使用域名调试本地项目
- Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]
- 开始做POI啦...
- ECshop网店系统百万级商品量性能优化-加快首页访问速度
- Android中为窗口定义主题
- 添加三维动画 demo
- shell 备份脚本
- Java sax、dom、pull解析xml
- 【2019雅礼集训】【CF 960G】【第一类斯特林数】【NTT&;多项式】permutation
- 转 Tomcat+redis+nginx配置
- 53_并发编程-线程-GIL锁
- Android : 基于alsa库的音乐播放
- 使用SignalR实时Web应用程序
- android studio 使用总结
- 加深Java基础,做了20道题选择题!简答题没做
- 真正理解 git fetch, git pull 以及 FETCH_HEAD(转)
- java Vamei快速教程08 继承
- SIGPIPE 13 和其他信号的对照表
- android 分享到QQ空间的全部操作