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

最新文章

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

热门文章

  1. 选中没有选中的复选框,匹配含有某个字符串的正则,json取值的两种方法,把变量定义在外面跟里面的区别
  2. SQL Server数据类型转换
  3. Face++ – 提供给你实时的脸部识别 API
  4. 12款最佳的 WordPress 语法高亮插件推荐
  5. win7系统下,vs2010一调式,vs就关闭要重启
  6. 轻松玩转jquery。
  7. datagridview的数据存取
  8. crm2013关于contentIFrame不能使用
  9. Web自动化测试 Selenium 3/3 https的配置
  10. Mac下的快速回到桌面快捷方式