Abstract: Application of map, sicp2.21

Source: Internet
Author: User

Abstract: Application of map, sicp2.21

Map was introduced at exercise 2.21. To be precise, map was introduced in the previous article.


To complete this exercise, or even to read this article later, you must understand the concept of map. As mentioned in the book, "map is a very important structure, not only does it represent a common mode, but it also creates a high-level abstraction for processing tables ".


Although it is very tall, it is not difficult to understand the concept of map. The most basic understanding is to give you a set of things. If you perform map operations on these things, it is to take out the Members in this group of things one by one and perform corresponding operations.


For example, map is used as a friend and a real tyrant .....


In addition, note that the returned value of map is returned. The map process returns a group. The number of group members is the same as that of the input group. The value of the group members depends on the corresponding operation.


For example, if map is a friend of mine, the returned result should be "Friends of local tyrants". The number is the same as that of "local tyrants", but one of them is your friend, because they all perform friend operations.


Exercise 2.21 requires that we accept a Value List, perform a square operation on each element, and then return the list of elements with the number of records.


Two implementation methods are provided in the question: map is not used, map is used, and some code frameworks are provided.


The code framework without map is as follows:

(define (square-list-1 items)  (if (null? items)      '()      (cons <??> <??>)))


Based on our previous experience in traversing the list, we know that recursion is required here, the first of which is <?> It should be the operation to sit down, the second <?> It should be recursively called square-list.

It is easy to do with the idea. The code after completion is as follows:


(define (square-list-1 items)  (if (null? items)      '()      (cons (* (car items) (car items))     (square-list-1 (cdr items)))))


In fact, if map is used, it is easier to use (lambda (x) (* x) as a map operation. The map process will traverse the list, take out each element and pass it

(Lambda (x) (* x) performs the square operation.

The complete code is as follows:

(define (square-list-2 items)  (map (lambda (x) (* x x) ) items))




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.