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. Asp.net 面向接口可扩展框架之核心容器(含测试代码下载)

    新框架的容器部分终于调通了!容器实在太重要了,所以有用了一个名词叫“核心容器”. 容器为什么那么重要呢?这个有必要好好说道说道. 1.首先我们从框架名称面向接口编程说起,什么是面向接口编程?(这个度娘 ...

  2. C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

    最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大 ...

  3. C# 时间戳转换为时间方法

            /// <summary>         /// 时间戳转为C#格式时间         /// </summary>         /// <par ...

  4. 15、ASP.NET MVC入门到精通——MVC-路由

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 Routing(路由) – URL url 作为广泛使用的Web用户接口,需要被重视 好的Url应该满足如下条件: URL应为获取某种资源提 ...

  5. 用好spring mvc validator可以简化代码

    表单的数据检验对一个程序来讲非常重要,因为对于客户端的数据不能完全信任,常规的检验类型有: 参数为空,根据不同的业务规定要求表单项是必填项 参数值的有效性,比如产品的价格,一定不能是负数 多个表单项组 ...

  6. jQuery获取Table-Input控件值封装

  7. 从CSS实现正片叠底看=>混合模式mix-blend-mode

    兼容性:这个东西说多了也没意思,像HTML5和CSS3这种兼容性时刻变化的东东,我们最好在自己支持的设备上实验,不支持,就在想办法呗,这个东西就是为了方便和好玩 所有属性: mix-blend-mod ...

  8. CSS手动改变DIV高宽

    本实例代码可以使DIV可以手动改变大小 效果体验:http://hovertree.com/code/css/resize.htm 代码如下: <!DOCTYPE html> <ht ...

  9. iOS 怎么设置 UITabBarController 的第n个item为第一响应者?

    iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...

  10. EA(企业架构)落地之道

    随着业务和IT的不断融合,企业业务.信息系统和技术结合日益紧密,企业对信息系统及技术的敏捷性要求越来越高.如何在多变的环境下快速创新产品或服务并推向市场是企业面临的日益紧迫的压力.企业架构作为连接和匹 ...