"SICP Exercise" 70 Exercise 2.41

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.