Scheme (3)

Source: Internet
Author: User

By freeuniverser

Scheme is logical. Maybe this is the charm of functional language ~A program of Scheme:

 1 ;square root 2  3 ;Newton's method 4  5 (define (sqrt x) 6  7     (define (square n) (* n n)) 8  9     (define (average x y)10 11         (/ (+ x y) 2))12 13     ;guess the square root14 15     ;improve the guess-method16 17     (define (improve guess x)18 19         (average guess (/ x guess)))20 21     ;a predetermined tolerance(here is 0.001)22 23     (define (good-enough? guess x)24 25         (< (abs (- (square guess) x)) 0.001))26 27     ;a radicand and guess a value as square root28 29     (define (sqrt-iter guess x)30 31         (if(good-enough? guess x)32 33             guess34 35             (sqrt-iter (improve guess x)36 37                           x)))38 39     ;guess the square root of any number is 1.040 41     (sqrt-iter 1.0 x))42 43 ;Procedural decomposition of the sqrt program.

 

 

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.