用的DrRacket平台
#lang racket
(* 5 6)
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
compound procedure
cond
(if <predicate> <consequent> <alternative>)
(define (abs x)
(if (< x 0)
(- x)
x))
注意负号与x是隔开的
Define a procedure that takes three numbers as arguments and returns the sum of the
squares of the two larger numbers.
找出三者中的最小的数,然后用三者的平方和减去最小数的平方即可
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (small x y)
(if (< x y)
x
y))
(define (square-sum x y z)
(+ (square x)
(square y)
(square z)
(- (square (small(small x y) z)))))
The contrast between function and procedure is a reflection of the general distinction between
describing properties of things and describing how to do things, or, as it is sometimes referred to,
the distinction between declarative knowledge and imperative knowledge. In mathematics we are
usually concerned with declarative (what is) descriptions, whereas in computer science we are
usually concerned with imperative (how to) descriptions.
newton-method to solve square roots
(define (square x) (* x x))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
the number of ways to change amount a using all but the first kind of coin, plus
the number of ways to change amount a - d using all n kinds of coins, where d is the
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount (- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
the standard Racket language (#lang racket
) will support internaldefine
s without problems.
#lang racket
(define (square x) (* x x))
(define (expmod base exp m)
(cond ((= 0 exp ) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m))
m))
(else
(remainder (* base (expmod base (- exp 1) m))
m))))
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(define (cube x) (* x x x))
(define (sum-integers a b)
(if (> a b)
0
(+ a (sum-integers (+ a 1) b))))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
(sum term (next a) next b))))next被inc代替,实际操作的只是a上,调用procedure a,(next a),不对b进行操作
lamdda构造procedure
(lambda (<formal-parameters>) <body>)
lambda构造的procedure没有名字
可以用lambda表达式然后结合参数算出表达式的值
((lambda (x y z) (+ x y z)) 4 5 6)
let表达式创建局部变量(local variable)
let表达式定义的变量范围(scope of let expression is the body of left)
finding fixed points of functions
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define dx 0.00001)
(define (cube x) (* x x x))
((deriv cube) 5)
procedure deriv的参数是procedure g,并且返回值也是procedure,即lambda表达式