Update scripts for next exercises
This commit is contained in:
@@ -355,7 +355,7 @@
|
|||||||
(b (analyze (if-fail-second exp))))
|
(b (analyze (if-fail-second exp))))
|
||||||
(lambda (env succeed fail)
|
(lambda (env succeed fail)
|
||||||
(a env
|
(a env
|
||||||
(lambda (a-value fail2) (succeed a-value fail))
|
(lambda (a-value fail2) (succeed a-value fail2))
|
||||||
(lambda () (b env succeed fail))))))
|
(lambda () (b env succeed fail))))))
|
||||||
|
|
||||||
;;; Amb-Eval input:
|
;;; Amb-Eval input:
|
||||||
@@ -368,22 +368,55 @@
|
|||||||
(require (even? x))
|
(require (even? x))
|
||||||
x)
|
x)
|
||||||
'all-odd)
|
'all-odd)
|
||||||
))
|
|
||||||
|
|
||||||
(assert (first result) 'all-odd)
|
|
||||||
|
|
||||||
(amball '(begin
|
|
||||||
(if-fail (let ((x (amb 1 3 5 8)))
|
(if-fail (let ((x (amb 1 3 5 8)))
|
||||||
(require (even? x))
|
(require (even? x))
|
||||||
x)
|
x)
|
||||||
'all-odd)
|
'all-odd)
|
||||||
|
|
||||||
))
|
))
|
||||||
|
|
||||||
(assert (first result) 8)
|
(assert result '(8 all-odd))
|
||||||
|
|
||||||
|
|
||||||
(display "\nex-4.53\n")
|
(display "\nex-4.53\n")
|
||||||
|
|
||||||
|
; (let ((pairs '()))
|
||||||
|
; (if-fail (let ((p (prime-sum-pair '(1 3 5 8) '(20 35 110))))
|
||||||
|
; (permanent-set! pairs (cons p pairs))
|
||||||
|
; (amb))
|
||||||
|
; pairs))
|
||||||
|
|
||||||
|
; The code will yield all prime-sum-pairs as expected.
|
||||||
|
|
||||||
|
(display "[answered]\n")
|
||||||
|
|
||||||
(display "\nex-4.54\n")
|
(display "\nex-4.54\n")
|
||||||
|
|
||||||
|
; Exercise 4.54. If we had not realized that require could be implemented as
|
||||||
|
; an ordinary procedure that uses amb, to be defined by the user as part of a
|
||||||
|
; nondeterministic program, we would have had to implement it as a special
|
||||||
|
; form. This would require syntax procedures
|
||||||
|
|
||||||
|
(define (require? exp) (tagged-list? exp 'require))
|
||||||
|
|
||||||
|
(define (require-predicate exp) (cadr exp))
|
||||||
|
|
||||||
|
; and a new clause in the dispatch in analyze
|
||||||
|
|
||||||
|
; ((require? exp) (analyze-require exp))
|
||||||
|
|
||||||
|
; as well the procedure analyze-require that handles require expressions.
|
||||||
|
; Complete the following definition of analyze-require.
|
||||||
|
|
||||||
|
(define (analyze-require exp)
|
||||||
|
(let ((pproc (analyze (require-predicate exp))))
|
||||||
|
(lambda (env succeed fail)
|
||||||
|
(pproc env
|
||||||
|
(lambda (pred-value fail2)
|
||||||
|
(if <??>
|
||||||
|
<??>
|
||||||
|
(succeed 'ok fail2)))
|
||||||
|
fail))))
|
||||||
|
|
||||||
|
(display "[done]\n")
|
||||||
|
|
||||||
4
ex-4_55-xx.scm
Normal file
4
ex-4_55-xx.scm
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
(load "util.scm")
|
||||||
|
|
||||||
|
(display "\nex-4.55\n")
|
||||||
|
|
||||||
Reference in New Issue
Block a user