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#基础-FileStream实现多线程断点续传
一.前言 网上有许多的多线程断点续传操作,但总是写的很云里雾里,或者写的比较坑长.由于这几个月要负责公司的在线升级项目,所以正好顺便写了一下 代码如下: using System; using Sys ...
- Composer Player 属性设置
/// <summary> /// 设置选中名称 /// </summary> /// <param name="name"></para ...
- IIS 配置Http重定向到Https
注意首先要安装url重定向模块 https://www.microsoft.com/zh-CN/download/details.aspx?id=7435 然后在web.config末尾添加如下 ...
- 【开源】SoDiaoEditor 可能是目前最好用的开源电子病历编辑器(B/S架构)
此刻我的内心是忐忑的,这个标题给了我很大的压力,虽然很久以前我就在github上搜索一圈了,也没发现有其他更好的开源电子病历编辑器,如各位亲发现有更好的,烦请知会我一声. 该编辑器其实已经憋了很久了, ...
- poj1228--稳定凸包
题目大意:给你一个凸包上的某些点(可能在凸包内),询问是否能确定这个凸包. 思路:先求出题目给出的点的凸包,看看在凸包的每条边内(不包括端点)有没有点,若有,则这条边是确定的,若没有,则这条边不确定, ...
- java 中包的概念,常量,静态与继承
一.包的概念:创建,使用. 1.新建包: 最上面一行,之前不能再有其它代码了. package 包名; 注意:包名一般都有命名的规范.例如:com.itnba.maya.zy(从大到小). 2.使用包 ...
- PowerDesigner 15设置mysql主键自动增长及基数
PowerDesigner 15设置mysql主键自动增长及基数 1.双击标示图,打开table properties->columns, 如图点击图标Customize Columns an ...
- javascript 实现一个回文数字
写一个方法,让"1234"变成回文数字“1234321”,就是顺着读和倒着读都是一样的:注:不让用reverse()方法: function palindrome(str){ va ...
- DYN-B201 Dynamics CRM 云生产力解决方案与功能简介
DYN-B201 Dynamics CRM 云生产力解决方案与功能简介 讲师:王健.林松涛Dynamics CRM 云产品正式落地中国,CRM 与 Azure.O365 深度整合无缝集成,带来无与伦比 ...
- Clion 跨平台的C++ IDE
CLion 是 JetBrains 推出的全新的 C/C++ 跨平台集成开发环境. 正式版本已经发出,目前是1.0.1 http://www.jetbrains.com/clion/ http://b ...