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 ...
随机推荐
- Gulp.js 参考手册,自动化构建利器
Gulp 是最新的基于 Node 的自动化构建工具,希望能够取代 Grunt,成为最流行的 JavaScript 任务运行器.通过结合 NodeJS 的数据流的能力,只需几步就能搭建起自己的自动化项目 ...
- RSuite 一个基于 React.js 的 Web 组件库
RSuite http://rsuite.github.io RSuite 是一个基于 React.js 开发的 Web 组件库,参考 Bootstrap 设计,提供其中常用组件,支持响应式布局. 我 ...
- HTML5拖拽实例
最近应该会用到,借用一下......小妹儿,你又变懒了 拖拽相关属性 draggable属性是html5的全局属性,是html5支持拖放操作的方式之一,用来表示元素是否可以被拖放,draggable有 ...
- sharepoint2013的最少下载策略概述(MDS)
该策略是 SharePoint 2013 中的一种新功能,通过在用户导航到新页面时仅发送差异内容来减少页面加载时间. 最少下载策略 (MDS) 是 SharePoint 2013 中的一种新技术,可减 ...
- Mac OX上安装MongoDb
MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码编译方式.我只想快速的装起来用一下,所以我选最简单的HomeBrew. 请参考官方文档 : http://do ...
- Android java传递string类型数据给C
本文接着实现<Android java传递int类型数据给C>的还未实现的方法: public native String sayHelloInC(String s); 先贴一个工具方法, ...
- 搭建Maven私服-续
前几天搭建了Maven私服,但是想在外网访问只能通过ip地址,因为公司用的不是固定ip所以,ip地址每次不一样,都要先打开极路由查看一下当前ip才能用,更恶心的是,代码check out只能一次,下次 ...
- 关于SQLSERVER2012版本远程登录问题
最近公司新配置了一台服务器,安装的数据库版本为sqlserver2012企业版本,一切安装正常,本地登录也正常 需要远程客户端登录,防火墙也开放的端口,路由器也做了端口映射,因为我们有两台服务器,14 ...
- 测试必备技能系列1 :通过mysql命令进行脚本数据导入
老徐,分享测试项目中实际能解决问题的干货! 今日分享: 如何通过mysql命令行,导入mysql脚本文件数据? ----- 解决实际的问题: 工作过程中,经常需要导入mysql脚本文件 很多同 ...
- HttpServletResponse对象 学习
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...