Exercise 4.61. The following rules implement a next-to relation that finds adjacent elements of a list:
(rule (?x next-to ?y in (?x ?y . ?u)))
(rule (?x next-to ?y in (?v . ?z)) (?x next-to ?y in ?z))
What will the response be to the following queries?
(?x next-to ?y in (1 (2 3) 4))
(?x next-to 1 in (2 1 3 1))
观察第一个 rule,
(rule (?x next-to ?y in (?x ?y . ?u)))
意思是,一个列表中起始的两个元素是相邻的元素,第二个 rule,
(rule (?x next-to ?y in (?v . ?z))
(?x next-to ?y in ?z))
意思是,要判断 ?x ?y 是否相邻,看 list 中第二个元素之后的元素。
所以,
(?x next-to ?y in (1 (2 3) 4))
会得到
(1 next-to (2 3) in (1 (2 3) 4))
((2 3) next-to 4 in ((2 3) 4))
而,
(?x next-to 1 in (2 1 3 1))
会得到
(2 next-to 1 in (2 1 3 1))
(3 next-to 1 in (3 1))