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. Javascript到PHP加密通讯的简单实现

    其实内容主要来源于上一篇博文,只是重新组织了语言,并做了原理性的阐述,更容易理解:P ----------------------------------------- 华丽的分割线 -------- ...

  2. linux系统免密码登陆

    有两台机器,系统都是CentOS6.5,IP分别为192.168.2.150,192.168.2.151.现在150需要SSH免密码登陆151. 在150上面执行命令,当前登录用户是root: # s ...

  3. tomcat通过socket连接MySQL,不再占用服务端口【linux】

    MySQL连接方式的说明 http://icbm.iteye.com/blog/1840673 MySQL除了最常见的TCP连接方式外,还提供SOCKET(LINUX默认连接方式).PIPE和SHAR ...

  4. Servlet基础(三) Servlet的多线程同步问题

    Servlet基础(三) Servlet的多线程同步问题 Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率. 由于Servlet/JSP默认是以多线程模式执行的, ...

  5. 操作系统开发系列—13.a.进程 ●

    进程的切换及调度等内容是和保护模式的相关技术紧密相连的,这些代码量可能并不多,但却至关重要. 我们需要一个数据结构记录一个进程的状态,在进程要被挂起的时候,进程信息就被写入这个数据结构,等到进程重新启 ...

  6. phonegap创建的ios项目推送消息出现闪退现象

    使用phonegap创建的ios项目,推送消息时,当程序在前台运行或者在后台运行状态下,推送消息过来,可以解析并且跳转: 但是在程序从后台退出的状态下,当消息推送过来的时候,点击通知栏,打开程序,程序 ...

  7. Java你可能不知道的事系列1

    概述 本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解.如果有不对的地方还请各位观众拍砖. 今天主要分享一下常用的字符串的几个题目,相信学习java的小伙伴们对String类是再熟 ...

  8. postgresql function 返回 select

    pq函数功能很强大,我打算把统计的功能都放在数据库端.优势让运算离数据更近一些,缺点无法服用代码.牺牲了django的灵魂性,项目必须依赖postgresql. 项目中,希望实现返回select内容 ...

  9. MVC中的时间js格式化

    记录下我遇到的一个,MVC中post请求返回一个JSON字符串,其中包含数据库中的时间格式(如:/Date(10000000000)/),不知道怎么处理.百度的方法都不适用,经自己研究,做成了一个Jq ...

  10. 4、界面前端设计师要阅读的书籍 - IT软件人员书籍系列文章

    前端工程师原来的职位是美工,原来只负责项目的一些简单网页制作,因为项目的需要,升级为前端工程师,这就涉及到JS等代码的编写了.前端工程师这个职位在目前来说算是新兴职位,在未来的几年里也是挺吃香的一个职 ...