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

ANSI Common Lisp Learn的更多相关文章

  1. 简体中文 — ANSI Common Lisp 中文版

    简体中文 - ANSI Common Lisp 中文版 简体中文¶

  2. ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版

    ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版 ANSI Common Lisp 中文翻譯版¶

  3. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  4. ANSI Common Lisp Practice - My Answers - Chatper - 2

    I work out the questions by myself Chapter 2 question 4. (defun greater (x y) (if (> x y) x y ) ) ...

  5. MAC 下用 Common Lisp 调试 OpenGL 程序

    MAC 下用 Common Lisp 调试 OpenGL 程序 环境搭建 运行环境: OSX 10.11.3 EI Capitan Common Lisp: SBCL 使用 SBCL, 首先要安装这几 ...

  6. 在windows上安装common lisp开发环境

    (2014.1写于CSDN的文章) 最近对lisp非常感兴趣,因此在google中搜索了“common lisp install windows”, 想装一个开发环境玩玩. 第一条结果就是 “Gett ...

  7. Common Lisp学习资源整理

    Lisp Hackers: Interviews with 100x More Productive Programmers Posted on June 26th, 2013 Lisp Hacker ...

  8. 一道Common Lisp 宏的练习题

    这是<ANSI Common Lisp>第10章“宏”的习题第6题: 定义一个宏,接受一变量列表以及一个代码主体,并确保变量在代码主体被求值后恢复 (revert)到原本的数值

  9. 搭建fedora开发环境 common lisp, c++, go

    第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...

随机推荐

  1. 分享11个纯css完成的图片浏览器

    图片画廊用于在网站上显示系列图片,它已成为网站重要的组成部分.实现图片画廊有很多种方法,今天要与大家分享的是11个使用纯 CSS 实现的图片画廊,它们代码少,效果炫,加载速度快,希望能对大家有所帮助. ...

  2. READ TABLE ..... BINARY SEARCH问题

    Read Table 的语法很多,这里说一种特殊情况,Read Table 中查询的时候对标准内表经常有一种二分优化查找,用Binary search的时候首先必须要有查询条件:但如果查询条件满足的项 ...

  3. 一次失败的APP业务渗透测试

    作者:whoamiecho 来源:ichunqiu 本文参加i春秋社区原创文章奖励计划,未经许可禁止转载! 一.  过程 1.1.事情起因:暴力破解 测试给了个普通用户账号,可以登录.APP一来就要登 ...

  4. 关于SharPoint2013一点细节的深究

    在进行SharePoint2013的开发过程中我发现在开启了某些功能,或者说是创建了个人站点之后有很多地方变了比如下面这个地方:     当然相应的URL地址也发生改变.也许很明确的我就打开了Welc ...

  5. jquery非空验证功能

    <script type="text/javascript">                $(function(){          /************* ...

  6. Failed to push selection: Read-only file system的解决方法

    1.获得root权限:adb root 2.设置/system为可读写:adb remount 3.将hosts文件复制到PC:adb pull /system/usr/keylayout/mtk-k ...

  7. Android jni helloworld

    新建Android项目,设置布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  8. Swift 二维码扫描 简单实现

    3.30看视频  学到了二维码简单的实现 还有一些动画的实现  今天就先记录一下二维码扫描的简单实现  不太好记手写一遍 学习的基础在于模仿嘛 创建一个实现二维码扫描的步骤 1.首先是懒加载创建 会话 ...

  9. linux 学习随笔-磁盘管理

    1:df 用于查看已挂载磁盘的容量信息 -i 查看inodes使用情况 -h 以合适的单位显示 -k -m 分别以k M单位显示 2:du 查看某个文件或者目录占用的空间 du [-abckmsh] ...

  10. js:插入节点appendChild insertBefore使用方法

    首先 从定义来理解 这两个方法: appendChild() 方法:可向节点的子节点列表的末尾添加新的子节点.语法:appendChild(newchild) insertBefore() 方法:可在 ...