1 #lang racket 2 3 ;;;;;;;;;;;;;;;;;2.40 4 (define nil '()) 5 6 (define (accumulate op intial seq) 7 (if (null? seq) 8 intial 9 (op (car seq) 10 (accumulate op intial (cdr seq))))) 11 12 (define (enumerate-interval low high) 13 (if (> low high) 14 nil 15 (cons low (enumerate-interval (+ low 1) high)))) 16 17 (define (make-pair-sum pair) 18 (list (car pair) (cadr pair) (+ (car pair) (cadr pair)))) 19 20 (define (prime-sum? pair) 21 (prime? (+ (car pair) (cadr pair)))) 22 23 (define (prime? n) 24 (define (test number) 25 (cond ((> (square number) n) #t) 26 ((= (remainder n number) 0) #f) 27 (else (test (+ number 1))))) 28 (test 2)) 29 30 (define (square x) 31 (* x x)) 32 33 (define (unique-pairs n) 34 (accumulate append 35 nil 36 (map (lambda (i) 37 (map (lambda (j) (list i j)) 38 (enumerate-interval 1 (- i 1)))) 39 (enumerate-interval 1 n)))) 40 41 ;;;;;;;;test 42 (unique-pairs 5) 43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 44 (define (prime-sum-pairs n) 45 (map make-pair-sum 46 (filter prime-sum? 47 (unique-pairs n)))) 48 49 ;;;;;;;;;test 50 (prime-sum-pairs 5)