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
)
)
question 5.
(a)
(defun enigma (x)
(and (not (null x))
(or (null (car x))
(enigma (cdr x))
)
)
)
; to judge whether there is a nil in x
(b)
(defun mystery (x y)
(if (null y)
nil
(if (eql (car y) x)
0
(let ((z (mystery x (cdr y))))
(and z (+ z 1))
)
)
)
)
; return the index of x in y (if not exist, return nil)
question 6.
(a) (car (car (cdr '(a (b c) d))))
(b) (or 13 (/ 1 0))
(c) (funcall (lambda (x y z) (list (car (funcall x y z)))) #'list 1 nil)
question 7.
; recursive version
(defun testlist (x)
(if (not (listp x))
nil ; not list, return nil
(if (null x)
nil ; is empty, return nil
(if (listp (car x))
t ; match the request, return true
(testlist (cdr x)) ; recurisvely test the rest
)
)
)
)
; iterative version
(defun testlist (x)
(if (not (listp x))
nil ; not list, return nil
(if (null x)
nil
(let ((res nil))
(dolist (obj x)
(if (listp obj)
(setf res t) ; res to record the judgement
nil
)
)
res ; return res
)
)
)
)
question 8.
(a)
; iterative
(defun PrintDot (x)
(do ((i 1 (+ i 1)))
((> i x) 'done)
(format t ".")
)
(format t "~%")
)
; recursive
(defun PrintDot (x)
(if (> x 0)
(progn
(format t ".")
(PrintDot (- x 1))
)
nil
)
)
(b)
; iterative
(defun testa (x)
(if (not (listp x))
nil
(let ((res 0))
(dolist (obj x)
(if (eql obj 'a)
(setf res (+ res 1))
)
)
res
)
)
)
; recursive
(defun testa (x)
(if (not (listp x))
nil
(if (null x)
0
(let
((z (if (eql (car x) 'a)
1
0
)))
(setf z (+ z (testa (cdr x))))
z
)
)
)
)
question 9.
(a)
(defun summit (lst)
(setf lst (remove nil lst))
(apply #'+ lst)
)
(b)
(defun summit (lst)
(if (null lst)
0
(let ((x (car lst)))
(if (null x)
(summit (cdr lst))
(+ x (summit (cdr lst)))
)
)
)
)
ANSI Common Lisp Practice - My Answers - Chatper - 2的更多相关文章
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- 简体中文 — ANSI Common Lisp 中文版
简体中文 - ANSI Common Lisp 中文版 简体中文¶
- ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版
ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版 ANSI Common Lisp 中文翻譯版¶
- 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 ...
- 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 ...
随机推荐
- C#播放MP3源代码
代码如下: using System; using System.Runtime.InteropServices; using System.Text; using System.IO ; using ...
- ASP.NET开源CMS
CMS这里指 内容管理系统.是Content Management System的缩写. 产生 随着网络应用的丰富和发展,很多网站往往不能迅速跟进大量信息衍生及业务模式变革的脚步,常常需要花费许多时间 ...
- Python 3.4 send mail
#coding=utf-8 #Python 3.4 https://docs.python.org/3.4/library/ #IDE:Visual Studio 2015 Window10 impo ...
- 【Java每日一题】20161209
package Dec2016; public class Ques1209 { public static void main(String[] args){ People g = new Peop ...
- 采花 bzoj 2743
采花(1s 128MB)flower [题目描述] 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整 ...
- 【http抓包】记录一次抓手机app的接口
抓手机的接口地址,好用的工具很多,想 windows下的 Fiddler 和mac下的Charles 1. fiddler的设置教程是 http://jingyan.baidu.com/article ...
- Visual Studio中UnitTesting单元测试模板代码生成
在软件研发过程中,单元测试的重要性直接影响软件质量.经验表明一个尽责的单元测试方法将会在软件开发的某个阶段发现很多的Bug,并且修改它们的成本也很低.在软件开发的后期阶段,Bug的发 ...
- mariadb 10.2.3支持延时复制
在mysql 5.6开始就支持延时复制,这在一些需要维护大量非标准化系统或者运维技术水平较低的公司和开发人员众多的项目组这是一个非常有价值的特性,可以说误操作的概率跟一个城市车祸概率的水平差不多了,我 ...
- 十一个行为模式之策略模式(Strategy Pattern)
定义: 定义一系列的算法,将每一个算法封装起来,并使它们之间可以相互替换,让算法具有可扩展性和对立性. 结构图: Context:环境类,算法的使用者.对外提供了算法使用的接口,并且持有一个抽象算法类 ...
- java多线程-读写锁
Java5 在 java.util.concurrent 包中已经包含了读写锁.尽管如此,我们还是应该了解其实现背后的原理. 读/写锁的 Java 实现(Read / Write Lock Java ...