Study Notes (P3-P17)

Source: Internet
Author: User

For more than a year, we feel that the content inside is worth reading. It's time to take action. Let's start with this article. Come on, stick to it.

Exercise answer: http://eli.thegreenplace.net/category/programming/lisp/sicp/

IDE: http://racket-lang.org/

Program Basic Design Elements

Basic Expressions are used to represent the simplest individual.

The composite elements can be constructed from a simple structure.

Abstract METHODS rename composite objects and operate them as units

General process definition form

General format of the definition process: (define (<Name> <formal paremeters>) <body>)

For example, define a square process.

(Define (square X) (* X ))

The square process can be used as the basic structure to form other processes, for example, to obtain the fourth power of a number.

 
(Define (double-square X) (square X )))
Conditional expressions

The general expression of the condition: (cond (<P1> <E1>) (<P2> <E2>)... (<PN> <en> ))

For example, calculate the absolute value of a number.

(Define (abs x) (cond (> x 0) X) (= x 0) 0) (<x 0) (-X ))))

In addition, you can use the if expression, which is applicable to analysis in only two cases. evaluate an absolute value of a number, either less than 0 to return its opposite number, or return its own.

 
(Define (f x) (if (<x 0) (-x) X ))
Practice 1.6

It seems that there is no problem according to the conventional thinking. I also thought about it for a while, and finally I found out why. in the original scheme, the cond expression executes each condition branch. if each line of Cond is added with a "break" similar to C #, this problem should be solved.

Exercise 1.7 combined with exercise 1.8
 (define (SQRT-begin guessvalue X) (Let (improved-guess (improve guessvalue X) (if (close-enough? Guessvalue improved-guess) improved-guess (SQRT-begin improved-guess X) (define (close-enough? A B) (LET (ratio (/a B) (and (
  
    Ratio 0.999) (define (cube X) (* X) (define (sqrts X) (SQRT-begin 1.0 X) (define (improve guessvalue X) (average (/X (* guessvalue )) (* 2 guessvalue) (define (average a B) (/(+ a B) 3) 
  

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.