]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex4_36.rkt
solutions to 4.35, 4.36 and 4.37
[sicp.git] / src / sicp / ex4_36.rkt
1 #lang racket
2
3 (require "amb-eli.rkt")
4
5 (define (an-integer-starting-from n)
6   (amb n (an-integer-starting-from (+ n 1))))
7
8 (define (an-integer-between low high)
9   (assert (< low high))
10   (amb low (an-integer-between (+ low 1) high)))
11
12 #|
13
14 If we replace an-integer-between with an-integer-starting-from, then
15 the k grows too high to invalid ranges and never stops. The way to accomplish
16 correct values is to restrict i, j and k to valid ranges.
17
18 |#
19
20 ;; using euclid's formula
21 ;; http://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple
22 (define (pythagorean-triples)
23   (let [(n (an-integer-starting-from 1))]
24     (let [(m (an-integer-starting-from n))]
25       (assert (> m n))
26       (list (- (sqr m) (sqr n))
27             (* 2 m n)
28             (+ (sqr m) (sqr n))))))