Scheme (6)

Source: Internet
Author: User

By freeuniverser

It is very convenient to use Scheme to write a numerical computation program. Functional language is an excellent tool for playing mathematical computation. It can solve many mathematical problems by definition. This is very flexible for coder.

For example, define a function.F (n), when n <3, f (n) = n; when n> = 3, f (n) = f (n-1) + 2f (n-2) + 3f (n-3). Therefore:

UseRecursionMethod:

1 (define (f n)2 3   (cond ((< n 3) n)4 5            ((>= n 3)6 7            (+ (f (- n 1)) (* 2 (f (- n 2)))8 9                (* 3 (f (- n 3)))))))

UseIterationMethod:

 1 (define (f n) 2  3    (define (f0 a b c count) 4  5       (if (= count 0) 6  7          c 8  9          (f0 (+ a (* 2 b) (* 3 c)) a b (- count 1))))10 11     (f0 2 1 0 n))

In this example, we can see thatRecursionRelatively clear, but its efficiency is not the latterIterationThe two ideas complement each other.

Next, let's take a look at the interesting classic questions:Fibonacci numbers

1 ;fibonacci numbers by recursion2 3 (define (fib n)4 5    (cond ((= n 0) 0)6 7             ((= n 1) 1)8 9             (else (+ (fib (- n 1)) (fib (- n 2))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;

 1 ;fibonacci numbers by iteration 2  3 (define (fib n) 4  5    (define (fib-iter a b count) 6  7       (if (= count 0) 8  9             b10 11             (fib-iter (+ a b) a (- count 1))))12 13   (fib-iter 1 0 n))

The following method is used to calculate ononacci In the logarithm step:

 1 (define (fib n) 2   (define (square n) (* n n)) 3   (define (fib-iter a b p q count) 4     (cond ((= count 0) b) 5           ((even? count) 6            (fib-iter a 7                      b 8                      (+ (square p) (square q)) 9                      (+ (* 2 p q) (square q))10                      (/ count 2)))11            (else (fib-iter (+ (* b q) (* a q) (* a p))12                            (+ (* b p) (* a q))13                            p14                            q15                            (- count 1)))))16     (fib-iter 1 0 0 1 n))

ComputingFactorialIt is also convenient to use Scheme:

1 ;factorial by recursion2 3 (define (factorial n)4 5     (if (= n 1)6 7         18 9         (* n (factorial (- n 1)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;

 1 ;factorial by iteration 2  3 (define (factorial n) 4  5      (define (iter product counter) 6  7         (if (> counter n) 8  9               product10 11               (iter (* product counter)12 13                       (+ counter 1))))14 15     (iter 1 1))

NextPascal triangle:

1 (define (pascal-triangle a b)2 3      (cond ((= a b) 1)4 5               ((= b 0) 1)6 7               (else (+ (pascal-triangle (- a 1) (- b 1))8 9                           (pascal-triangle (- a 1) b)))))

The computing power of Scheme is not small. Its syntax is clear and elegant ~

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.