Practice 4.36
简单的替换an-integer-between为an-integer-starting-from后为:
(define (a-pythagorean-triple)
(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))))))
这种情况下,在第内层let中的无限种可能,会导致程序永远不会回溯到上一层let去选择其他可能性。所以内层的组合应该是有限的集合,让程序可以回溯到上层,才能遍历完所有的组合。改进后如下:
(define (a-pythagorean-triple)
(let ((i (an-integer-starting-from 1)))
(let ((j (an-integer-between 1 i)))
(let ((k (an-integer-between 1 j)))
(require (= (* i i) (+ (* j j) (* k k))))
(list k j i)))))