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. 如何让C#像JavaScript一样编程

    JavaScript是一门动态语言,可以动态的给对象添加属性和方法,非常方便.那么有没有一种方式可以让C#也具备动态添加属性和方法的能力,像Javascript一样进行编程? 下面就介绍一个很不错的框 ...

  2. php随机生成指定长度的字符串 可以固定数字 字母 混合

    php 生成随机字符串 可以指定是纯数字 还是纯字母 或者混合的. 可以指定长度的. function rand_zifu($what,$number){ $string=''; for($i = 1 ...

  3. kindeditor-在线编辑器

    写在前面的话: 今天是第一次写博客,很值得纪念,希望能够和大神们一起交流技术,一起进步...来自<一只有梦想的前端小白> 最近项目中需要实现图文混排的效果,所以研究了下在线编辑器-- ki ...

  4. Android 进程生命周期 Process Lifecycle

    Android 进程生命周期 Process Lifecycle 进程的生命周期 Android系统会尽力保持应用的进程,但是有时为了给新的进程和更重要的进程回收一些内存空间,它会移除一些旧的进程. ...

  5. iOS 7 与 Xamarin - MultiPeer Connectivity(转载)

    随着时代的改变,移动设备在生活工作都开始取代原有的pc.设备间的数据交互就成为了必备可少的功能.比较成熟的产品有NFC.这个啪啪的操作很流行,例如分享图片,分享文件等 .但是在iOS设备中还没有NFC ...

  6. Android Studio简单设置

    IDE外观&行为 修改主题,修改全局字体 修改主题,想用炫酷的深色主题,就改成Darcula吧:字体的话,选一个带中文的,要不然会有很多口口,我这里用Microsoft YaHei UI,很不 ...

  7. 【代码笔记】iOS-点击加号增加书架,点击减号减少书架

    一,效果图. 二,工程图. 三,代码. ReaderViewController.h #import <UIKit/UIKit.h> @interface ReaderViewContro ...

  8. Html5 dataset--自定义属性

    dataset--自定义属性 HTMLElement.dataset data-*属性集 元素上保存数据 <div id="user" data-id="12345 ...

  9. DOM 节点操作

    一.获取节点 方法名 只能document调用 返回单一的值 返回动态集合 getElementById √ √ getElementsByTagName √ getElementsByClassNa ...

  10. UML类图的关系

    多态 泛化(Generalization) [定义]:是一种继承关系,表示一般与特殊的关系,它指定了子类如何特化父类的所有特征和行为 [UML表示]:带三角箭头的实线,箭头指向父类 [代码表现]:A类 ...