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. C#使用Graphics画圆写字

    画填充圆: Graphics gra = this.pictureBox1.CreateGraphics(); gra.SmoothingMode = System.Drawing.Drawing2D ...

  2. ASP.NET MVC 了解FileResult的本质

    FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端.ASP.NET MVC定义了三个具体的FileResult,分 ...

  3. Top 15 不起眼却有大作用的 .NET功能集

    目录 1. ObsoleteAttribute2. 设置默认值属性: DefaultValueAttribute3. DebuggerBrowsableAttribute4. ??运算符5. Curr ...

  4. proxool+hsqldb数据库

    使用hsqldb数据库,开发环境eclipse,jdk8.0 Dsqldb数据库 1.下载包:hsqldb.jar 2.新建项目,复制到项目WebContent -> WEB -> INF ...

  5. ASP.NET MVC搭建项目后台UI框架—3、面板折叠和展开

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  6. Atitit利用反射获取子类 集合 以及继承树

    Atitit利用反射获取子类 集合 以及继承树 想从父类往下找子类的确是不可能的,要知道只要类不是final的话谁都有继承它的自由不需要事前通知父类. Eclipse实现不是重父类开始找而是重子类往回 ...

  7. MVC Razor语法

    Razor语法, 视图引擎 Razor(CSHTML) @ 可以编写一条C#语句@{} 可以编写一组C#语句,也有可能嵌着Html@: 将文字内容直接输出到页面上去@() 在一句中将一段C#代码包括起 ...

  8. 原生JS实战:分享一个首页进度加载动画!

    本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5871134.html 该程序是本人的个人作品,写的不好,可以参考,但未经 ...

  9. 网页端实现input数字输入框

    实现input输入框只能输入数字的效果: <input type="text" name="" id="phoneNum" value ...

  10. dede:field name='imgurls'不能二次使用的解决办法

    {dede:field name='imgurls' alt='图片输出区'}图片链接  [field:linkurl/]图片地址 [field:imgsrc/]{/dede:field} 这个标签不 ...