Skip to content

Commit

Permalink
add 4.55--4.60
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Apr 10, 2016
1 parent bab3e53 commit cb2f53c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 2016-04/2016-04-09_logical_programming.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## 内容

4.4 逻辑程序设计

## 内容

- 程序设计语言要求程序员以一种形式去表述有关的知识,其中需要指明一种为解决某一特定问题的一步一步的方法。
- 面向表达式的语言(如 Lisp、Fortran 和 Algol)利用了表达式的“一语双关”:一个描述了某个函数值的表达式也可以解释为一种计算该值的方法。正由于此,大部分程序设计语言都强烈的倾向于单一方向的计算。
- 逻辑程序设计扩展了程序设计就是关于如何构造出计算单向函数的算法的观点,提出了一种程序设计的关系模型,其中加入了一类功能强大的称为`合一`的符号模式匹配。
- Hewitt 1969 认识到,我们有可能将`程序设计语言的控制结构``完成逻辑操作的系统中的运算`结合起来,由此导致了4.3.1小节提到的自动搜索方面的工作。
- 当代的逻辑程序设计语言都有一些实质性的缺陷,它们里面有关“怎样做”的通用方法,有可能使它们陷入缪误性的无穷循环或者其他并非我们期望的行为之中。
12 changes: 12 additions & 0 deletions exercises/04/4.55.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; a) 所有被 Ben Bitdiddle 管理的人

(supervisor ?name (Ben Bitdiddle))

; b) 会计部所有人的名字和工作

(job ?name (accounting . ?type))


; c) 在Slumerville 居住的所有人的名字和住址。

(address ?name (Slumerville . ?street-address))
17 changes: 17 additions & 0 deletions exercises/04/4.56.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; a) Ben Bitdiddle 的所有下属的名字,以及他们的住址

(and (supervisor ?name (Ben Bitdiddle))
(address ?name ?address))

; b) 所有工资少于 Ben Bitdiddle 的人,以及他们的工资和 Ben Bitdiddle 的工资

(and (salary (Ben Bitdiddle) (?Ben-salary))
(salary ?x ?others-salary)
(list-value > ?Ben-salary ?others-salary))

; c) 所有不是由计算机分部的人管理的人,以及他们的上司和工作

(and
(not (job ?name (computer . ?title)))
(supervisor ?name ?supervisor)
(job ?supervisor ?supervisor-job))
16 changes: 16 additions & 0 deletions exercises/04/4.57.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

(rule (can-replace p1 p2)
(or (and (job p1 ?job1)
(job p2 ?job2)
(or (same? job1 job2)
(can-do-job job1 job2))
(not (same p1 p2)))))

; a)
(can-replace ?x (Fect Cy D))

; b)
(and (can-replace ?p1 ?p2)
(salary ?p1 ?salary1)
(salary ?p2 ?salary2)
(lisp-value < ?salary1 ?salary2))
5 changes: 5 additions & 0 deletions exercises/04/4.58.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(rule (big-shop ?person ?division)
(and (job ?person (?division . ?type))
(supervisor ?person ?supervisor)
(job ?supervisor (?division2 . ?type2))
(not (same (?division2 ?division)))))
13 changes: 13 additions & 0 deletions exercises/04/4.59.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; a)
(meeting ?range (Friday ?time))

; b)
(rule (meeting-time ?person ?day-and-time)
(and
(job ?person (?division . ?type))
(or (meeting ?division ?day-and-time)
(meeting whole-company ?day-and-time))))


; c)
(rule (meeting-time (Hacker Alyssa P) (Wednesday ?time)))
28 changes: 28 additions & 0 deletions exercises/04/4.60.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```
(rule (lives-near ?p1 ?p2)
(and (address ?p1 (?town . ?rest-1))
(address ?p2 (?town . ?rest-2))
(not (same ?p1 ?p2))))
(rule (same ?x ?x))
```
当用`(lives-near ?p1 ?p2)`去匹配所有居住的比较近的人时,每对人之所以会出现两次,是因为`lives-near`中的`same`是符合交换律的。

如果想要每对人只列出一次,必须找出个不满足交换律(像大于、小于)的操作替换掉`same`


```
(defun greater-as-string (obj1 obj2)
(string>
(write-to-string obj1)
(write-to-string obj2)))
(rule (lives-near ?p1 ?p2)
(and (address ?p1 (?town . ?rest-1))
(address ?p2 (?town . ?rest-2))
(lisp-value #'greater-as-string ?p1 ?p2)))
```
参考:

- http://eli.thegreenplace.net/2008/02/08/sicp-section-441/

0 comments on commit cb2f53c

Please sign in to comment.