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 ...
随机推荐
- 使用VS Code开发调试.NET Core 多项目
使用Visual Studio Code(VS Code)开发调试.NET Core和ASP.NET Core 多项目multiple project. 之前讲解过如果使用Visual Studio ...
- 模仿迅L看看<音频播放器> 实现点击进度条,跳转播放
<Style x:Key="btnFallback" TargetType="{x:Type Button}"> <Setter Proper ...
- CSS3梅花三弄特效
效果预览:http://hovertree.com/texiao/js/22/ 效果图: 代码如下: <html> <head> <meta http-equiv=&qu ...
- 一台主机上安装多个Tomcat
1. 下载解压版的Tomcat,并解压两次,分别命名为Tomcat_Server_01和Tomcat_Server_02: 2. 进入Tomcat_Server_01\bin目录,编辑service文 ...
- 基于Netbeans的PHPUnit单元测试环境搭建
一.配置 PHPUnit截至2015-10-16,稳定版已更新至5.0.6,要求使用PHP v5.6及以上的环境才能使用. PHPUnit的4.8系列要求在PHP v5.3.3以上环境使用. Netb ...
- PHP流程控制结构之分支结构
流程控制对于任何一门编程语言来说都是具有通用与普遍性的,是程序的重要组成部分.可以这么说,在任何一门程序设计语言中,都需要支持三种基本结构:顺序结构.分支结构(选择结构或条件结构)和循环结构.对于顺序 ...
- python征程1.2(初识python)
1.操作符. 和其他绝大多数语言一样,Python的算术操作符以你熟悉的方式工作. “+,—,×,/,//,%,**,” 注意:python有两种除法操作符(1)单斜杠用以传统除法,(2)双斜杠用以浮 ...
- 一元多项式的乘法与加法运算(C语言)
输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式: 输出分2行,分别以指数递降方 ...
- 【夯实PHP基础】PHP的date函数
本文地址 原文地址 提纲: 1. 引言 2. 代码示例 3. 参考资料 1. 引言 今天看到一段代码 $timeNew = date('n月j日', strtotime($oldTime)); 感觉有 ...
- 使用C#开发数据库应用系统
第一章 初识Windows程序 01.浅谈控制台应用 解析:控制台应用程序:dos窗口中显示 Windows窗体应用程序:有控件参与的,支持事件的一种程序 02.关于窗体项目的注意点: /*01.一个 ...