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 ...
随机推荐
- caching与缓存
通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.例如,如果应用程序使用复杂的逻辑来处理大量数据,然后再将数据作为用户频繁访问的报表返回,避免在用 ...
- 工厂模式 - Factory
简单工厂模式 SimpleFactory Pattern,将一个具体类的实例化交给一个静态工厂方法来执行. 特点: 增加功能需要修改工厂类,扩展性较差: 参考: 设计模式学习笔记 - 简单工厂模式: ...
- HttpClient通过Post上传多个文件
public static String sendFilesPost(String url, String fileNames) { HttpClient httpClient = null; Htt ...
- Lock、ReentrantLock、synchronized、ReentrantReadWriteLock使用
先来看一段代码,实现如下打印效果: 1 2 A 3 4 B 5 6 C 7 8 D 9 10 E 11 12 F 13 14 G 15 16 H 17 18 I 19 20 J 21 22 K 23 ...
- 从零开始学 Java - CentOS 安装 JDK
我来总结一下吧 昨天我写了一篇从零开始学 Java - 我放弃了 .NET ?,在园子里突然引起了强烈的讨论,有期待我能持续更新的.有鼓励支持的.有相同经历的.也有同一个学校的师兄弟(我们相认了).当 ...
- 【开发软件】推荐一款MAC OS X 下php集成开发环境mamp
这里给大家推荐一款在mac上搭建WEB服务器环境的集成环境安装软件,非常的好用,需要的朋友可以拿去,不用谢 ^_^ 之前苦于mac上搭建本地服务器之艰辛,找寻好久都没找到一款类似windows ...
- 获取SqlServer存储过程定义的3种方法
第一种: declare @p_text varchar(max) SELECT @p_text= text FROM syscomments WHERE id = ( SELECT id FROM ...
- App Store2016年最新审核规则
为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同. 以下规则和示例旨在帮助开发者 ...
- 把Sharepoint Desinger 工作流部署到生产环境
下面是比较简单的方法,把Designer工作流从开发环境部署到生产环境. 在Sharepoint Desinger 2013 中点击需要部署的工作流. 点击保存,发布. 点Export to Visi ...
- 用Kotlin开发Android应用(II):创建新项目
这是关于Kotlin的第二篇.各位高手发现问题,请继续“拍砖”. 原文标题:Kotlin for Android(II): Create a new project 原文链接:http://anton ...