SICP/ex-4_35-xx.scm
2021-01-31 11:53:16 -05:00

51 lines
1.4 KiB
Scheme

(load "util.scm")
(define (require p)
(if (not p) (amb)))
(define (an-element-of items)
(require (not (null? items)))
(amb (car items) (an-element-of (cdr items))))
(define (an-integer-starting-from n)
(amb n (an-integer-starting-from (+ n 1))))
(display "\nex-4.35 - an-integer-between\n")
(define (an-integer-between a b)
(require (<= a b))
(amb a (an-integer-between (+ a 1) b)))
(define (a-pythagorean-triple-between low high)
(let ((i (an-integer-between low high)))
(let ((j (an-integer-between i high)))
(let ((k (an-integer-between j high)))
(require (= (+ (* i i) (* j j)) (* k k)))
(list i j k)))))
(display "[done]\n")
(display "\nex-4.36 - all-pythagorean-triples\n")
; If we replace an-integer-between with an-integer-starting-from the variables
; i and j will stay at their initial value 1 while k will increment endlessly.
; Hence, only triplets of the form (1 1 n) will be generated.
(define (all-pythagorean-triples)
(let ((i (an-integer-starting-from 1)))
(let ((j (an-integer-starting-from i)))
(let ((k (an-integer-starting-from j)))
(require (= (+ (* i i) (* j j)) (* k k)))
(list i j k)))))
; Write a procedure that actually will accomplish this. (That is, write a
; procedure for which repeatedly typing try-again would in principle eventually
; generate all Pythagorean triples.)
; (display "[done]\n")
(display "\nex-4.37\n")
;(display "\nex-4.38\n")