SICP -- Building Abstractions With Procedures

;; Building Abstractions With Procedures

( define ( my-sqrt x )

( define ( good-enough? guess )

( < ( abs ( - ( square guess ) x ) ) 0.001 ) )

( define ( improve guess )

( average guess ( / x guess ) ) )

( define ( sqrt-iter guess )

( if ( good-enough? guess )

guess

( sqrt-iter ( improve guess ) ) ) )

( sqrt-iter 1.0 ) )

;; fac

( define ( fac n )

( if ( = n 1 ) 1

( * n ( fac ( - n 1 ) ) ) ) )

( define ( fac1 n )

( define ( fac-iter product counter )

( if ( > counter n ) product

( fac-iter ( * counter product )

( + counter 1 ) ) ) )

( fac-iter 1 1 ) )

;; Ackerman

( define ( Ackerman x y )

( cond ( ( = y 0 ) 0 )

( ( = x 0 ) ( * 2 y ) )

( ( = y 1 ) 2 )

( else ( Ackerman ( - x 1 )

( Ackerman x ( - y 1 ) ) ) ) ) )

;; Fibonacci

( define ( fib n )

( cond ( ( = n 0 ) 0 )

( ( = n 1 ) 1 )

( else ( + ( fib ( - n 1 ) )

( fib ( - n 2 ) ) ) ) ) )

( define ( fib1 n )

( define ( fib-iter a b counter )

( if ( = counter 0 ) b

( fib-iter ( + a b ) a ( - counter 1 ) ) ) )

( fib-iter 1 0 n ) )

;; Counting change

( define ( count-change amount )

( 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 ) ) )

;; Exponentiation

( define ( my-expt b n )

( if ( = n 0 ) 1

( * b ( my-expt b ( - n 1 ) ) ) ) )

( define ( my-even? n )

( = ( remainder n 2 ) 0 ) )

( define ( fast-expt b n )

( cond ( ( = n 0 ) 1 )

( ( my-even? n ) ( square ( fast-expt b ( / n 2 ) ) ) )

( else ( * b ( fast-expt b ( - n 1 ) ) ) ) ) )

;; Greatest Common Divisors

( define ( my-gcd a b )

( if ( = b 0 )

a

( my-gcd b ( remainder a b ) ) ) )

;; Testing for Primality

( define ( is-prime? num )

( define ( test-div div )

( cond ( ( >= div num ) #t )

( ( = ( remainder num div ) 0 ) #f )

( else ( test-div ( + div 2 ) ) ) ) )

( cond ( ( = ( remainder num 2 ) 0 ) #f )

( else ( test-div 3 ) ) ) )

;; Searching for divisors

( define ( square n )

( * n n ) )

( define ( smallest-divisor n )

( find-divisor n 2 ) )

( define ( find-divisor n test-divisor )

( cond ( ( > ( square test-divisor ) n ) n )

( ( divides? test-divisor n ) test-divisor )

( else ( find-divisor n ( + test-divisor 1 ) ) ) ) )

( define ( divides? a b )

( = ( remainder b a ) 0 ) )

( define ( prime? n )

( = n ( smallest-divisor n ) ) )

;; Procedures as Arguments

( define ( cube x )

( * x x x ) )

( define ( sum term a next b )

( if ( > a b )

0

( + ( term a )

( sum term ( next a ) next b ) ) ) )

( define ( pi-sum low-limit high-limit )

( sum ( lambda ( x ) ( / 1.0 ( * x ( + x 2 ) ) ) )

low-limit

( lambda ( x ) ( + x 4 ) )

high-limit ) )

( define ( sum term low-limit next high-limit )

( if ( > low-limit high-limit )

0

( + ( term low-limit )

( sum term ( next low-limit ) next high-limit ) ) ) )

( define ( integral func low-limit high-limit dx )

( * ( sum func

( + low-limit ( / dx 2.0 ) )

( lambda ( x ) ( + x dx ) )

high-limit )

dx ) )

;;Constructing Procedures Using Lambda

( ( lambda ( x )

( + ( let ( ( x 3 ) )

( + x ( * x 10 ) ) )

x ) ) 5 )

( ( lambda ( x y )

( let ( ( x 3 )

( y ( + x 2 ) ) )

( * x y ) ) ) 2 10 )

;; f( x, y ) = x( 1 + xy ) ^ 2 + y( 1 - y ) + ( 1 + xy )( 1 - y s)

;; a = 1 + xy

;; b = 1 - y

;; f( x, y ) = x * a ^ 2 + y * b + a * b

( define ( func x y )

( let ( ( a ( + 1 ( * x y ) ) )

( b ( - 1 y ) ) )

( + ( * x ( square a ) )

( * y b )

( * a b ) ) ) )

;; Finding roots if equations by the half-interval method

( define ( average a b )

( / ( + a b ) 2.0 ) )

( define ( close-enough? x y )

( < ( abs ( - x y ) ) 0.001 ) )

( define ( search func neg-point pos-point )

( let ( ( mid-point ( average neg-point pos-point ) ) )

( if ( close-enough? neg-point pos-point )

mid-point

( let ( ( test-value ( func mid-point ) ) )

( cond ( ( positive? test-value )

( search func neg-point mid-point ) )

( ( negative? test-value )

( search func mid-point pos-point ) )

( else mid-point ) ) ) ) ) )

( define ( half-interval-method func a b )

( let ( ( a-value ( func a ) )

( b-value ( func b ) ) )

( cond ( ( and ( negative? a-value )

( positive? b-value ) )

( search func a b ) )

( ( and ( negative? b-value )

( positive? a-value ) )

( search func b a ) )

( else

( error "Values are not of opposite sign." ) ) ) ) )

( half-interval-method

( lambda ( x )

( - ( * x x x ) ( * 2 x ) 3 ) )

1.0

2.0 )

;; The Fermat test:

( define ( bad-exp base exp )

( cond ( ( = exp 0 ) 1 )

( ( even? exp )

( square ( bad-exp base ( / exp 2 ) ) ) )

( else ( * base ( bad-exp base ( - exp 1 ) ) ) ) ) )

( define ( bad-expmod base exp m )

( remainder ( bad-exp base exp ) m ) )

( define ( expmod base exp m )

( cond ( ( = exp 0 ) 1 )

( ( even? exp )

( remainder

( square ( expmod base ( / exp 2 ) m ) )

m ) )

( else

( remainder

( * base ( expmod base ( - exp 1 ) m ) )

m ) ) ) )

;; ( expmod 32 10911110033 10911110033 )

;; ( my-expmod 32 1091111003 1091111003 )

( 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 ) ) )

;; Finding fixed points of functions

( define tolerance 0.00001 )

( define ( good-enough? v1 v2 )

( < ( abs ( - v1 v2 ) ) tolerance ) )

( define ( fixed-point func now-guess )

( let ( ( next-guess ( func now-guess ) ) )

( cond ( ( good-enough? now-guess next-guess ) now-guess )

( else ( fixed-point func next-guess ) ) ) ) )

( fixed-point cos 1.0 )

( fixed-point ( lambda ( x ) 

                 ( + ( sin x ) ( cos x ) ) ) 1.0 )

;; The result in an infinite loop in which

;; the two guesses y1 and y2 repeat over and over

;; oscillating about the answer

;; one way to control such oscillations is to

;; prevent the guesses from changing so much.

;; Since the answer is always between our guess y and x/y

;; we can make a new guess that is not as far from y as x/y

;; by average y with x/y

;; so that the next guess after y is ( y + x/y )/2 instead od x/y

;; The process of making such a sequence of guesses is simply the

;; process of looking for a fixed point of y |-> ( y + x/y )/2

( define ( my-sqrt x )

( fixed-point ( lambda ( y )

( average y ( / x y ) ) ) 1.0 ) )

( my-sqrt 4 )

;; Procedures as Returned Values

( define ( average-damp func )

( lambda ( x )

( average x ( func x ) ) ) )

( ( average-damp square ) 10 )

( define ( my-sqrt x )

( fixed-point ( average-damp ( lambda ( y )

( / x y ) ) ) 1.0 ) )

( my-sqrt 4 )

;; Newton‘s method

;; taking dx to be, say 0.00001

( define dx 0.00001 )

( define ( deriv func )

( lambda( x )

( / ( - ( func ( + x dx ) ) ( func x ) ) dx ) ) )

( define ( cube x )

( * x x x ) )

( ( deriv cube ) 5 )

( define ( newton-transform func )

( lambda ( x )

( - x ( / ( func x ) ( ( deriv func ) x ) ) ) ) )

( define ( newton-method func guess )

( fixed-point ( newton-transform func ) guess ) )

( define ( my-sqrt x )

( newton-method ( lambda ( y )

( - ( square y ) x ) ) 1.0 ) )


SICP -- Building Abstractions With Procedures

时间: 2024-08-02 07:01:10

SICP -- Building Abstractions With Procedures的相关文章

1 Building Abstractions with Functions

Every powerful language has three such mechanisms: primitive expressions and statements, which represent the simplest building blocks that the language provides, means of combination, by which compound elements are built from simpler ones, and means

2. Building Abstractions with Data

程序的构造: 将数据抽象(data abstraction), 将操作data的过程用函数抽象(function abstraction) abstraction barriers 访问数据的某些属性时就用相应的方法,而不是从数据的构造方法入手 sequence processing: sequence iteration list comprehensions aggregation: aggregate all values in a sequence into a single value

SICP 第一章 构造过程抽象 1

Chapter 1 Building Abstractions with Procedures .title { text-align: center } .todo { font-family: monospace; color: red } .done { color: green } .tag { background-color: #eee; font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal

高阶函数之函数作为参数

SICP 1.3.1  Procedures as Arguments,说明高阶函数之函数作为参数的原因:若干个函数拥有相似的算法或代码结构,对此加以抽象. (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) 于是: 清单1: (defi

[SICP Notes] 1.1 The Elements of Programming

About the Book To know more about programming, I have decided to start reading the famous Structure and Interpretation of Computer Programs (SICP, or the Wizard Book), which is first published by MIT in 1985. The Wizard Book used MIT Scheme, which is

Easy and cheap cluster building on AWS backup

https://grapeot.me/easy-and-cheap-cluster-building-on-aws.html Thu 17 July 2014 , by Yan Wang | 2 Comments Linux Parallel github Image Why? It often requires a lot of computational resources to do machine learning / computer vision research, like ext

Building a choropleth map of Italy using mapIT(转)

In the R environment, different packages to draw maps are available. I lost the count by now; surely, spand ggmap deserve consideration. Despite the great availability of R functions dedicated to this topic, in the past, when I needed to draw a very

Putting Apache Kafka To Use: A Practical Guide to Building a Stream Data Platform-part 1

转自: http://www.confluent.io/blog/stream-data-platform-1/ These days you hear a lot about "stream processing", "event data", and "real-time", often related to technologies like Kafka, Storm, Samza, or Spark's Streaming module.

The Building Blocks- Components of EA Part 2- Process, People, Network and Time

1. Zachman Framework Information (Data) - Answer the Question 'What?' Contextual: List of Things important to the Business Conceptual: Semantic Model - Taxonomy Logical:  Entity Relationship Diagram Physical: Physical Data Model As Built: Data Dictio