/
ch4-3-setup-repl.scm
69 lines (55 loc) · 2.06 KB
/
ch4-3-setup-repl.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
; This file is used to save me some typing into the interpreter.
(define (load-definition input)
(define (internal-loop try-again)
(if (eq? input 'try-again)
(try-again)
(begin
(ambeval input
the-global-environment
;; ambeval success
(lambda (val next-alternative) '())
;; ambeval failure
(lambda ()
(announce-output
";;; There are no more values of")
(user-print input))))))
(internal-loop
(lambda ()
(newline)
(display ";;; There is no current problem"))))
(define the-global-environment (setup-environment))
(load-definition '(define (require p)
(if (not p) (amb))))
(load-definition '(define (an-element-of items)
(require (not (null? items)))
(amb (car items) (an-element-of (cdr items)))))
(load-definition '(define (a-random-element-of items)
(require (not (null? items)))
(ramb (car items) (a-random-element-of (cdr items)))))
(load-definition '(define (a-random-integer-starting-from n)
(ramb n (a-random-integer-starting-from (+ n 1)))))
(load-definition '(define (a-random-integer-between low high)
(if (not (>= low high))
(ramb low (a-random-integer-between (+ low 1) high))
low)))
(load-definition '(define (test-randomization)
(let ((a (a-random-integer-between 1 10)))
(require (> a 0))
a)))
(load-definition '(define (square x) (* x x)))
(load-definition '(define (smallest-divisor n)
(find-divisor n 2)))
(load-definition '(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
(( divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1))))))
(load-definition '(define (divides? a b)
(= (remainder b a) 0)))
(load-definition '(define (prime? n)
(= n (smallest-divisor n))))
(load-definition '(define (prime-sum-pair list1 list2)
(let ((a (an-element-of list1))
(b (an-element-of list2)))
(require (prime? (+ a b)))
(list a b))))
;(driver-loop)