ANSI Common Lisp Learn
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的更多相关文章
- 简体中文 — ANSI Common Lisp 中文版
简体中文 - ANSI Common Lisp 中文版 简体中文¶
- ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版
ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版 ANSI Common Lisp 中文翻譯版¶
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- 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 ) ) ...
- MAC 下用 Common Lisp 调试 OpenGL 程序
MAC 下用 Common Lisp 调试 OpenGL 程序 环境搭建 运行环境: OSX 10.11.3 EI Capitan Common Lisp: SBCL 使用 SBCL, 首先要安装这几 ...
- 在windows上安装common lisp开发环境
(2014.1写于CSDN的文章) 最近对lisp非常感兴趣,因此在google中搜索了“common lisp install windows”, 想装一个开发环境玩玩. 第一条结果就是 “Gett ...
- Common Lisp学习资源整理
Lisp Hackers: Interviews with 100x More Productive Programmers Posted on June 26th, 2013 Lisp Hacker ...
- 一道Common Lisp 宏的练习题
这是<ANSI Common Lisp>第10章“宏”的习题第6题: 定义一个宏,接受一变量列表以及一个代码主体,并确保变量在代码主体被求值后恢复 (revert)到原本的数值
- 搭建fedora开发环境 common lisp, c++, go
第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...
随机推荐
- Quartz.NET开源作业调度框架系列(五):AdoJobStore保存job到数据库
Quartz.NET 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger(用于定义调度时间的元素,即按照什么时间规则去执行任务) 和 job 是任务调度的元数 ...
- One Page Scroll – 实现苹果风格的单页滚动效果
单页滚动网站已经被广泛使用了有一段时间了,它们对于快速提供信息是很有用的.One Page Scroll 是一个 jQuery 插件,简化了创建此类网站的步骤,只需创建 HTML 结构,进行简单设置, ...
- C#关于word文档的书签替换操作
public void Get_Word(string gjbh) { try { DataSet ds = OperaterBase.GetDsBySql("select diffTabl ...
- URL-统一资源定位器
URL - Uniform Resource Locator URL 可以由单词组成,比如 “w3school.com.cn”,或者是因特网协议(IP)地址:192.168.1.253.大多数人在网上 ...
- 刷新ALV定位到当前记录行
如果使用"REFRESH_TABLE_DISPLAY"刷新ALV后,记录会跳到第一行,以下代码可以使记录仍然定位在当前行 DATA ls_stable TYPE lvc_s_stb ...
- Vault插件示例--Vault Explorer与Thin Client的集成。
Autodesk Vault 2014的Subscription 包中有一个组件叫做Thin Client.这个瘦客户端有着全新的界面,又给了我们一个全新的选择.ThinClient实际是在Vault ...
- IOS常用第三方开源类库&组件
1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功能强大, 现在许多人 ...
- sqlite与多线程
数据库支持三种线程模式 Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in mor ...
- mac 下如何切换jdk的版本
1.打开.bash_profile文件添加一个函数 #add a function for switch idk version.function jdkset() { if [ $# -ne 0 ] ...
- NSCache详解
NSCache---详解 NSCache: NSCache是苹果官方提供的缓存类,在AFNetworking中,使用它来进行图片缓存. NSCache是线程安全的,在多线程操作中,不需要对Cache进 ...