ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead.
1
(a)
(b)
(c)
(d)
2
注:union 在 Common Lisp 中的作用就是求两个集合的并集。但是这有一个前提,即给的两个列表已经满足集合的属性了。具体的操作过程似乎是对第一个 list 中的每一个元素在第二个 list 中查找,如无则标记一下;待第一个 list 的所有元素在第二个 list 中查完以后将所有标记过的元素放入一个 list 中与第二个 list 进行合并。这意味着,如果刚开始给的两个 list 不完全满足集合的属性,则会有重复出现。本题依照这种思路来完成。
(defun new-union (lst1 lst2)
(let ((tmplst '()))
(dolist (obj lst2)
(if (not (member obj lst1))
(setf tmplst (append tmplst (cons obj '())))
nil))
(append lst1 tmplst)))
3
(defun occurrences (lst)
(let ((tmplst '()))
(dolist (obj lst)
(if (assoc obj tmplst)
(setf tmplst (addcount obj tmplst))
(setf tmplst (append tmplst (cons (cons obj 1) nil)))))
(sort tmplst #'(lambda (lst1 lst2) (> (cdr lst1) (cdr lst2))))))
(defun addcount (obj tmplst)
(let (reslst '())
(dolist (xobj tmplst)
(if (equal (car xobj) obj)
(setf reslst (append reslst (cons (cons obj (+ (cdr xobj) 1)) nil)))
(setf reslst (append reslst (cons xobj '())))))
reslst))
4
'(a) 不是 atom,'a 才是 atom。而 member 是采用 eql 判断相等,不是用 equal。个人感觉 (remove '(a . 2) '((a . 2) (b . 3))) 达不到效果也是这个原因。
P.S. 后来在读到第四章时发现,这个问题可以添加 关键字参数 :test #'equal 来解决。(英文版 67 页)
5
; iteration
(defun pos+1 (lst)
(let ((tmplst nil) (n 0))
(dolist (obj lst)
(setf tmplst (append tmplst (cons (+ obj n) nil)))
(setf n (+ n 1)))
tmplst))
; recursion
(defun pos+2 (lst)
(wtf lst 0))
(defun wtf (lst n)
(if (null lst)
nil
(cons (+ (car lst) n) (wtf (cdr lst) (+ n 1)))))
; mapcar (In fact, I also use iteration to create a list from 0 to n-1...)
(defun pos+3 (lst)
(mapcar #'+ lst
(let ((tmplst nil) (n 0))
(dolist (obj lst)
(setf tmplst (append tmplst (cons n nil)))
(setf n (+ n 1)))
tmplst)))
6
注:第一个 cons 的改写不太明白怎么做。cons 已经是基本的操作符之一了。另外第三个 list 的改写需要用到后面的函数接受不定参数。根据知识屏蔽原则,先标记上,将来学到那里了再反过头来补上。再,刚开始定义的 cdr-x 和 car-x 均为题中“政府”制定的 cdr 和 car。
(defun cdr-x (lst)
(car lst))
(defun car-x (lst)
(cdr lst))
(defun cons-new (obj lst)
())
(defun length-new (lst)
(if (null lst)
0
(+ 1 (length-new (car-x lst)))))
(defun member-new (obj lst)
(if (null lst)
nil
(if (eql obj (cdr-x lst))
lst
(member-new obj (car-x lst)))))
7
仅仅修改了 n-elts 函数,使之返回一个 dotted list 而不是一个 proper list
; modified version
(defun compress-m (x)
(if (consp x)
(compr-m (car x) 1 (cdr x))
x))
(defun compr-m (elt n lst)
(if (null lst)
(list (n-elts-m elt n))
(let ((next (car lst)))
(if (eql next elt)
(compr-m elt (+ n 1) (cdr lst))
(cons (n-elts-m elt n)
(compr-m next 1 (cdr lst)))))))
(defun n-elts-m (elt n)
(if (> n 1)
(cons n elt) ; modify here
elt))
8
(defun showdots (lst)
(let ((n (length lst)))
(dolist (obj lst)
(format t "(~A . " obj))
(format t "NIL")
(do ((i 1 (+ i 1)))
((> i n) 'nil)
(format t ")"))))
9
前注:一开始感觉在 3.15 节那个 bfs 算法上改动一下,使当前 node 等于 end 并且 (cdr queue) 为 nil 时再返回,这样可以找到 longest path。然而为了避免在环路中无终止,需要再加上两个限制,都是针对 new-paths 函数的:1. 如果当前带入的 node 是 end 的话,就废弃这条路,不再返回;2. 如果非 1 中情况,先把 mapcar 映射得到的列表搞出来,把其中存在重复元素的子列表删去(即环路),再返回给 bfs 函数。
(defun shortest-path (start end net)
(bfs end (list (list start)) net))
(defun bfs (end queue net)
(if (null queue)
nil
(let ((path (car queue)))
(let ((node (car path)))
(if (and (eql node end) (null (cdr queue)))
(reverse path)
(bfs end
(append (cdr queue)
(new-paths path node end net))
net))))))
(defun new-paths (path node end net)
(if (equal node end) ; if node = end, abandon it
nil
(let ((tmplst (mapcar #'(lambda (n) (cons n path)) (cdr (assoc node net))))
(reslst nil))
(dolist (obj tmplst)
(if (> (no-repeat obj) (length obj)) ; only return the path which has no repeat dots
(setf reslst (cons obj reslst))
nil))
reslst)))
(defun no-repeat (lst) ; judge whether there are repeat dots in a path list. if yes, return number less than or equal to length(lst); or return length(lst)+1
(let ((tmplst nil))
(do ((i 1 (+ i 1)))
((or (member (nth (- i 1) lst) tmplst) (> i (length lst))) i)
(setf tmplst (cons (nth (- i 1) lst) tmplst)))))
用于测试的网络:1.
'((a b c) (b c) (c d) (d b))
2.'((a b) (b e) (e f c) (c b d f) (f c))
后注:体会到了 lisp 调试的好。直接在 clisp 里边 load 一下,然后直接顶层调用相关函数,就可以调试了,速度很快。
ANSI Common Lisp Practice - My Answers - Chatper - 3的更多相关文章
- 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 ) ) ...
- 简体中文 — 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 ...
随机推荐
- 【Win10 应用开发】自定义应用标题栏
Win 10 app对窗口标题栏的自定义包括两个层面:一是只定义标题中各部分的颜色,如标题栏上文本的颜色.三个系统按钮(最大化,最小化,关闭)的背景颜色等:另一层是把窗口的可视区域直接扩展到标题栏上, ...
- cookie——小甜品
Cookie最早是网景公司的前雇员Lou Montulli在1993年3月的发明.Cookie英文意指饼干,不过在电脑术语中它可不像饼干那么简单.简单的说,Cookie就是服务器暂存放在你计算机上的一 ...
- 推荐几款jquery图片切换插件
一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...
- 用枚举enum替代int常量
枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...
- NodeJs对Mysql封装
之前在学习NodeJs的时候,每次操作数据库都需要连接数据库然后开始写Sql操作,这样非常麻烦,然后自己对Mysql进行了封装,一共100多行代码. github地址: Mysql操作 我在里面对My ...
- CSS知识总结(三)
CSS的常用样式 1.字体样式 1)字体名称(font-family) font-family : <family-name> 设置文字名称,可以使用多个名称,或者使用逗号分隔,浏览器 ...
- CloudNotes之桌面客户端篇:插件系统的实现
[CloudNotes版本更新历史与各版本下载地址请点击此处] [CloudNotes中文系列文章汇总列表请点击此处] [查看CloudNotes源代码请点击此处] 有时候,同一个名词,针对不同的人群 ...
- 谈谈React那些小事
前言 说起React,那也是近一年多时间火起来的前端框架,其在Facebook的影响力和大力推广下,已然成为目前前端界的中流砥柱.在如今的前端框架界,React.Vue.Angular三分天下的时代已 ...
- 在Winform界面菜单中实现动态增加【最近使用的文件】菜单项
在我们一些和文件处理打交道的系统中,我们往往需要记录下最近使用的文件,这样方便用户快速打开之前浏览或者编辑过的文件,这种在很多软件上很常见,本文主要介绍在Winform界面菜单中实现[最近使用的文件] ...
- python学习笔记(基础一:'hello world'、变量、字符编码)
第一个python程序: Hello World程序 windows命令行中输入:python,进入python交互器,也可以称为解释器. print("Hello World!" ...