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的更多相关文章

  1. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  2. 简体中文 — ANSI Common Lisp 中文版

    简体中文 - ANSI Common Lisp 中文版 简体中文¶

  3. ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版

    ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版 ANSI Common Lisp 中文翻譯版¶

  4. 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 ...

  5. MAC 下用 Common Lisp 调试 OpenGL 程序

    MAC 下用 Common Lisp 调试 OpenGL 程序 环境搭建 运行环境: OSX 10.11.3 EI Capitan Common Lisp: SBCL 使用 SBCL, 首先要安装这几 ...

  6. 在windows上安装common lisp开发环境

    (2014.1写于CSDN的文章) 最近对lisp非常感兴趣,因此在google中搜索了“common lisp install windows”, 想装一个开发环境玩玩. 第一条结果就是 “Gett ...

  7. Common Lisp学习资源整理

    Lisp Hackers: Interviews with 100x More Productive Programmers Posted on June 26th, 2013 Lisp Hacker ...

  8. 一道Common Lisp 宏的练习题

    这是<ANSI Common Lisp>第10章“宏”的习题第6题: 定义一个宏,接受一变量列表以及一个代码主体,并确保变量在代码主体被求值后恢复 (revert)到原本的数值

  9. 搭建fedora开发环境 common lisp, c++, go

    第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...

随机推荐

  1. 简析.NET Core 以及与 .NET Framework的关系

    简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core的到来 1. Runtime 2. Unified BCL 3. W ...

  2. iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

    iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...

  3. 创业公司招php商城开发者

    众筹  电商 已经融资100W美元 职位要求1.对PHP编程熟悉程度以上,有电商相关开发经验优先:2.熟悉lnmp相关配套搭建运维,开发;熟悉linux 使用3.对数据结构和算法设计有较深刻的理解:4 ...

  4. 将GridView数据导入到excel,并提供下载

    之前项目中需要一个导出数据到Excel的功能,现在将代码记录下来.其实将girdView中的数据导出的代码很简单,如下: ) { Response.Charset="GB2312" ...

  5. JQuery 快速入门

    1.要学习Jquery @首先要在需要的页面引入 <script type="text/javascript" src="jquery.js">&l ...

  6. 妈妈再也不用担心我找不到spring源码了!

    获取spring源码: http://repo.springsource.org/libs-release-local/ http://repo.springsource.org/libs-relea ...

  7. 用JS做一个简单的电商产品放大镜功能

    使用js制作一个简单的产品放大图 购物网站的产品页经常会放有一个产品展示图区.该图区有一个功能就是产品图的放大功能,移动左侧的焦点区域,可以放大细节部分观看,详情如下图.实现该功能的方法也非常简单. ...

  8. SDWebImage添加header

    title: SDWebImage添加headerdate: 2016-03-07 17:32:57tags: SDWebImagecategories: IOS keywords: SDWebIma ...

  9. 我的Android第五章:通过Intent实现活动与活动之间的交互

    Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互              同时Inten ...

  10. 实现Discuz论坛客户端应用源码

    通过使用该源码,开发者可以迅速地将Discuz论坛迁移到Android客户端中.不需要任何的开发工作即可拥有属于自己论坛的Android客户端 源码下载:http://code.662p.com/vi ...