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# 删除字符串中的中文
/// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete中文(string str) { s ...
- 纯css3艺术文字样式效果代码
效果:http://hovertree.com/texiao/css3/1/ 本效果主要使用text-shadow实现.参考:http://hovertree.com/h/bjaf/css3_text ...
- 一款css3很美的iphone注册表单样式
代码如下,保存到html文件打开: <!DOCTYPE html> <html lang=""> <head> <title>Ani ...
- java timer 执行任务
1. 建立timer import java.util.Timer; import java.util.TimerTask; public class Start { public class Sta ...
- Spring注入中byType和byName的总结
1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...
- ExtJS关于组件Component生命周期
extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有必须的配置设定.事件注册.预渲染处理等都在此时进行. 1.应用组件的配置: 当初始化一个 ...
- java web学习总结(二十) -------------------监听器属性详解
一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...
- angular源码分析:angular中脏活累活的承担者之$interpolate
一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...
- html5 canvas简易版捕鱼达人游戏源码
插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...
- js 假值
function demo(a){ if(a){ console.log(111); }else{ console.log(222); } } demo(0) html_dom.html:27 222 ...