大小: 2658
备注:
|
大小: 3464
备注:
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 73: | 行号 73: |
1 1. {{{ | 1. {{{ |
行号 127: | 行号 127: |
行号 141: | 行号 140: |
1. {{{ (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) }}}{{{#!python abs = lambda x: x if x > 0 else ( 0 if x == 0 else (-x if x < 0 else 0)) }}}或者{{{#!python abs = lambda x: x if x > 0 else (0 if x == 0 else -x) }}} 1. {{{ (define (abs x) (cond ((< x 0) (- x)) (else x))) }}} {{{#!python abs = lambda x: -x if x < 0 else x }}} 1. {{{ (define (abs x) (if (< x 0) (- x) x)) }}}{{{ abs = lambda x: -x if x < 0 else x }}} 1. {{{ (and (> x 5) (< x 10)) }}}{{{#!python x > 5 and x < 10 }}} 1. {{{ (define (>= x y) (or (> x y) (= x y))) }}}{{{#!python greater_or_equal = lambda x, y: x>y or x==y }}} 1. {{{ (define (>= x y) (not (< x y))) }}}{{{#!python greater_or_equal = lambda x, y: not x < y }}} |
Building Abstractions with Procedures
1. The Elements of Programming
1.1. Expressions
486
1 486
(+ 137 349) 486 (- 1000 334) 666 (* 5 99) 495 (/ 10 5) 2 (+ 2.7 10) 12.7
(+ 21 35 12 7) 75 (* 25 4 12) 1200
或者
reduce(int.__add__, [21, 35, 12, 7]) reduce(int.__mul__, [25, 4, 12])
(+ (* 3 5) (- 10 6)) 19
1 (3 * 5) + (10 - 6)
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
1 (3 * ((2*4)+(3+5)) ) + ((10-7)+6)
1.2. Naming and the Environment
(define size 2)
1 size = 2
size 2 (* 5 size) 10
(define pi 3.14159) (define radius 10) (* pi (* radius radius)) 314.159 (define circumference (* 2 pi radius)) circumference 62.8318
1.3. Evaluating Combinations
(* (+ 2 (* 4 6)) (+ 3 5 7))
1 (2+(4*6)) * (3+5+7)
1.4. Compound Procedures
(define (square x) (* x x))
1 square = lambda x: x*x
(square 21) 441 (square (+ 2 5)) 49 (square (square 3)) 81
(define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4) 25
(define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5) 136
1.5. The Substitution Model for Procedure Application
1.6. Conditional Expressions and Predicates
(define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x))))
1 abs = lambda x: x if x > 0 else ( 0 if x == 0 else (-x if x < 0 else 0))
或者
1 abs = lambda x: x if x > 0 else (0 if x == 0 else -x)
(define (abs x) (cond ((< x 0) (- x)) (else x)))
1 abs = lambda x: -x if x < 0 else x
(define (abs x) (if (< x 0) (- x) x))
abs = lambda x: -x if x < 0 else x
(and (> x 5) (< x 10))
1 x > 5 and x < 10
(define (>= x y) (or (> x y) (= x y)))
1 greater_or_equal = lambda x, y: x>y or x==y
(define (>= x y) (not (< x y)))
1 greater_or_equal = lambda x, y: not x < y
1.7. Example: Square Roots by Newton's Method
1.8. Procedures as Black-Box Abstractions
2. Procedures and the Processes They Generate
- 1.2.1 Linear Recursion and Iteration 1.2.2 Tree Recursion 1.2.3 Orders of Growth 1.2.4 Exponentiation 1.2.5 Greatest Common Divisors 1.2.6 Example: Testing for Primality
- 1.3 Formulating Abstractions with Higher-Order Procedures
- 1.3.1 Procedures as Arguments 1.3.2 Constructing Procedures Using Lambda 1.3.3 Procedures as General Methods 1.3.4 Procedures as Returned Values