Chapter 4, Metalinguistic Abstraction

Exercise 4.62


Code:

1
2
3
(assert! (rule (last-pair (?x . ()) (?x . ()))))
(assert! (rule (last-pair (?x ?y . ?z) ?r)
			   (last-pair (?y . ?z) ?r)))

Output/Test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;;; Query input:
(last-pair (3) ?x)

;;; Query results:
(last-pair (3) (3))

;;; Query input:
(last-pair (1 2 3) ?x)

;;; Query results:
(last-pair (1 2 3) (3))

;;; Query input:
(last-pair (2 ?x) (3))

;;; Query results:
(last-pair (2 3) (3))

;;; Query input:

On executing (last-pair ?x (3)) the code outputs an error: ;Aborting!: maximum recursion depth exceeded

In last case, I wonder how it even matches because there is nothing to match against! I guess the reason for it going to so much recursion can be answered only after understanding the query evaluator implementation.