sicp/chapter1.scm

248 lines
5.1 KiB
Scheme
Raw Permalink Normal View History

2019-03-18 16:14:09 -04:00
#lang sicp
(define (average x y) (/ (+ x y) 2))
(define (square x) (* x x))
(define (cube x) (* x x x))
(define (% a b)
(remainder a b))
;(define (sqrt x)
; (define (good-enough? guess)
; (< (abs (- (square guess) x)) 0.00001))
; (define (improve guess)
; (average guess (/ x guess)))
; (define (sqrt-iter guess)
; (if (good-enough? guess)
; guess
; (sqrt-iter (improve guess))))
; (sqrt-iter 1.0))
;(define (factorial n)
; (if (= n 1) 1 (* n (factorial (- n 1)))))
(define (factorial n)
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
(fact-iter 1 1 n))
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
;(define (fib n)
; (cond ((= n 0) 0)
; ((= n 1) 1)
; (else (+ (fib(- n 1))
; (fib(- n 2))))))
(define (fib n)
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(fib-iter 1 0 n))
(define (count-change amount)
(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)))
(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)))))
(cc amount 5))
(define (ptri n)
(if (< n 3) n
(+
(ptri (- n 1))
(+ (* (ptri (- n 2)) 2)
(* (ptri (- n 3)) 3)))))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine angle)
(if (not (> (abs angle) 0.1))
angle
(p (sine (/ angle 3.0)))))
;(define (expt b n)
; (if (= n 0) 1 (* b (expt b (- n 1)))))
(define (expt b n) (expt-iter b n 1))
(define (expt-iter b counter product)
(if (= counter 0)
product
(expt-iter b
(- counter 1)
(* b product))))
(define (fast-exp b n)
(cond ((= n 0)
1)
((even? n)
(square (fast-exp b (/ n 2))))
(else
(* b (fast-exp b (- n 1))))))
(define (gcd a b)
(if (= b 0)
a
(gcd b (% a b))))
(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)
(= (% b a) 0))
(define (prime? n)
(= n (smallest-divisor n)))
(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))))
(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 (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime ( - (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
;(define (sum-integers a b)
; (if (> a b)
; 0
; (+ a (sum-integers (+ a 1) b))))
;(define (sum-cubes a b)
; (if (> a b)
; 0
; (+ (cube a) (sum-cubes (+ a 1) b))))
;(define (pi-sum a b)
; (if (> a b)
; 0
; (+ (/ 1.0 (* a (+ a 2)))
; (pi-sum (+ a 4) 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))
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
(define (integral f a b dx)
(* (sum f (+ a (/ dx 2.0))
(lambda (x) (+ x dx))
b)
dx))
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))
(define tolerance 0.00001)
(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 (average-damp f)
(lambda (x)
(average x (f x))))
(define dx 0.00001)
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define (newton-transform g)
(lambda (x)
(- x (/ (g x)
((deriv g) x)))))
(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))
(define (sqrt x)
(newtons-method
(lambda (y)
(- (square y) x))
1.0))