2.40 Exercise 2.40
This procedure is almost identical to the one embedded in the original
prime-sum-pairs procedure, with one small change: The outer map (over
values of i) now enumerates from 2 to n, not from 1. The
inner enumerate-interval for j would produce an empty list (since it
would go from 1 to 0), which would get ignored when flatmap
appends the results together ((append nil nil) is still nil). A
clever trick knowing how flatmap works, but I prefer it this way. The
definition of the unique pairs given in the book states that i > 1 anyway.
| (define (unique-pairs n) | | (map | | (lambda (i) | | (map (lambda (j) (list i j)) | | (enumerate-interval 1 (- i 1)))) | | (enumerate-interval 2 n))) |
|
prime-sum-pairs now looks like this:
| (define (prime-sum-pairs n) | | (map make-pair-sum | | (filter prime-sum? (unique-pairs n)))) |
|