From the section called Nested Mappings
Exercise 2.40
Define a procedure unique-pairs that, given an integer
n, generates the sequence of pairs (i,j) with1 < j < i < n. Use unique-pairs to simplify the definition of prime-sum-pairs given above.
I wrote the following:
(define (prime-sum-pairs n)
(filter (lambda (seq)
(prime? (+ (car seq) (cadr seq))))
(unique-pairs n)))
(define (enumerate-integers start end)
(if (>= start end)
(list end)
(cons start (enumerate-integers (+ 1 start) end))))
(define (unique-pairs n)
(flat-map (lambda (i)
(map (lambda (j) (list i j))
(enumerate-integers 1 (- i 1))))
(enumerate-integers 2 n)))
(define (filter test-fn seq)
(if (null? seq) null
(if (test-fn (car seq))
(cons (car seq)
(filter test-fn (cdr seq)))
(filter test-fn (cdr seq)))))
(define (accumulate op initial seq)
(if (null? seq)
initial
(op (car seq)
(accumulate op initial (cdr seq)))))
(define (flat-map f seq)
(accumulate append
null
(map (lambda (x) (f x)) seq)))
(define (prime? n) (= (smallest-divisor n) n))
(define (divisible? n i) (= 0 (remainder n i)))
(define (square x) (* x x))
(define (smallest-divisor n)
(define (rec i)
(cond ((> n (square i)) n)
((divisible? n i) i)
(else (rec (+ 1 i)))))
(rec 2))
Can this be improved in any way?