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. Codrops 实验:使用 Vibrant.js 提取图像颜色

    Codrops 分享了一个有趣的颜色提取实验.这个想法是创建图像的调色板,既有图像本身的潜移默化的影响,也有一些花哨的颜色延伸.通过使用 Vibrant.js 来提取图像中的颜色,并通过 CSS 过滤 ...

  2. MySQL之MySQL5.7安装包(msi文件)在Windows8下安装

    最近自己在使用MySQL5.7.16.msi安装MySQL.自己下载的是.msi文件,在安装的过程中遇到了许多文件,网上大部分的Blog都是关于免安装包的安装方法,希望我的方法对大家有帮助. 1,下载 ...

  3. 【经验之谈】前端面试知识点总结(CSS相关)——附答案

    目录 二.CSS部分 1.解释一下CSS的盒子模型? 2.请你说说CSS选择器的类型有哪些,并举几个例子说明其用法? 3.请你说说CSS有什么特殊性?(优先级.计算特殊值) 4.要动态改变层中内容可以 ...

  4. SAP ALV内嵌(In-place)Excel的问与答

    1.问题:点击ALV工具栏的"Excel"图标后,出现空白的内嵌Excel界面,无法正常显示报表数据.可按以下思路解决:(1)检查Excel中的宏安全设置选项.访问方法:启动Exc ...

  5. sap去除后缀0方法

    原贴地址:http://fuhesap.com/SAP/179.html SHIFT str LEFT DELETING LEADING '0'.如果要在layout显示不出前面的0 格式: & ...

  6. ae_datagridview显示属性

    public partial class FrmAttributeTable : Form { private AxMapControl m_MapCtl; public FrmAttributeTa ...

  7. SharePoint 判断用户是否在字段"人员和组"里面

    两个自己平时写的方法,记录下来,方便以后查找使用: 1.判断用户是否在字段人员和组里面: public static bool IsUserInFiled(int UserID, string Lis ...

  8. [javascript svg fill stroke stroke-width rx ry ellipse 属性讲解] svg fill stroke stroke-width ellipse 绘制椭圆属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  9. cacti监控windows服务器

    参考文献: 通​过​C​a​c​t​i​监​控​w​i​n​d​o​w​s​资​源 前提条件 一.已安装好Cacti:ubuntu下cacti安装配置 二.准备好以下安装文件: Cacti_SNMP_ ...

  10. Android Studio 更换国内源下载依赖库

    我的博客:http://daycoding.com 小小程序猿 由于国内GFW的原因,经常导致android studio 莫名其妙的编译不了,多数原因是由于不能下载依赖库 Gradle支持三种不同的 ...