Practice 2.41
This problem is actually a variant of the prime example in the book, the essence of which is the same. So we're going to do it in the same order. First, let's complete the ternary group that produces 3 distinct integers. In the previous question, however, we have written a two-tuple that can produce 2 different integers. So as long as we produce a more I, so that it and the resulting two-tuple combination, it can produce ternary group. So, let's get started.
(Define (unique-triples N)
(Flatmap (Lambda (i)
(Map (Lambda (j)
(Cons i J))
(Unique-pairs (-I 1))))
(enumerate-interval 1 N)))
Then you need to define a predicate to determine if the sum of the ternary is equal to s. The example in the book also has a prime-sum? predicate is not.
(Define (triples-sum. Triples s)
(= s (+ (car triples)
(Cadr triples)
(caddr Triples))))
By querying the data, there are also readers who have made a clever process.
(Define (triples-sum. Triples s)
(= SUM (fold-right + 0 triples)))
It's really a ingenious.
Then you need a function that can filter out the non-conforming ternary group.
(Define (filter-triples s triples)
(Filter (lambda (x)
(triples-sum x s))
Triples))
As a result, the function described in title can be generated.
"SICP Exercise" 70 Exercise 2.41