blob: f1e706ffac1171cf862f3e56e70e9ade5d5c2a9b (
plain)
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
|
#lang racket
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms)
(+ this-coeff (* x higher-terms)))
0
coefficient-sequence))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
null
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define (count-leaves t)
(accumulate + 0 (map (lambda (x)
(if (not (pair? x))
1
(count-leaves x))) t)))
(define (flatmap proc seq)
(accumulate append null (map proc seq)))
(define (prime? x) (= (modulo x 2) 1))
(define (prime-sum? pair)
(prime? (+ (car pair) (cadr pair))))
(define (make-pair-sum pair)
(list (car pair) (cadr pair) (+ (car pair) (cadr pair))))
(define (enumerate-interval low high)
(if (> low high)
null
(cons low (enumerate-interval (+ 1 low) high))))
(define (unique-pairs n)
(flatmap (lambda (i)
(map (lambda (j) (list j i))
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 n)))
(define (prime-sum-pairs n)
(map make-pair-sum
(filter prime-sum? (unique-pairs n))))
(define (is-eq-s? s triplet)
(= s (accumulate + 0 triplet)))
(define
|