This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (cons a b) | |
(* (expt 2 a) (expt 3 b))) | |
(define (car z) | |
(define (recr a c) | |
(if (= (gcd z a) a) | |
(recr (* a 2) (+ c 1)) | |
(- c 1))) | |
(recr 1 0)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (cdr z) | |
(z (lambda (p q) q))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((lambda (m) (m x y) (lambda (p q) p))) | |
((lambda (p q) p) x y) | |
x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-rect2 p1 p2) | |
(let ((p3 (make-point (x-point p2) (y-point p1)))) | |
(cons (make-segment p1 p3) (make-segment p3 p2)))) | |
(define (width2 r) | |
(let ((seg (car r))) | |
(- (x-point (end-segment seg)) (x-point (start-segment seg))))) | |
(define (length2 r) | |
(let ((seg (cdr r))) | |
(- (y-point (end-segment seg)) (y-point (start-segment seg))))) | |
(define (area2 r) (* (width2 r) (length2 r))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-rect p1 p2) (make-segment p1 p2)) | |
(define (width r) (- (x-point (end-segment r)) (x-point (start-segment r)))) | |
(define (length r) (- (y-point (end-segment r)) (y-point (start-segment r)))) | |
(define (area r) (* (width r) (length r))) | |
(define (perimeter r) (* (+ (width r) (length r)) 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-segment p1 p2) (cons p1 p2)) | |
(define (start-segment s) (car s)) | |
(define (end-segment s) (cdr s)) | |
(define (mid-segment s) | |
(let ((x1 (x-point (start-segment s))) | |
(y1 (y-point (start-segment s))) | |
(x2 (x-point (end-segment s))) | |
(y2 (y-point (end-segment s)))) | |
(make-point (/ (+ x1 x2) 2) (/ (+ y1 y2) 2)))) | |
(define (print-segment s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-point x y) (cons x y)) | |
(define (x-point p) (car p)) | |
(define (y-point p) (cdr p)) | |
(define (print-point p) | |
(newline) | |
(display "(") | |
(display (x-point p)) | |
(display ",") | |
(display (y-point p)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Assume n and d are never 0, else gcd will return 0, which causes division by zero | |
(define (make-rat n d) | |
(define g (gcd n d)) | |
(if (or (and (< n 0) (< d 0)) (and (> n 0) (> d 0))) | |
(cons (/ (abs n) g) (/ (abs d) g)) | |
(cons (/ n g) (/ d g)))) | |
(define (numer x) (car x)) | |
(define (denom x) (cdr x)) | |
(define (print-rat x) | |
(display (numer x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (fixed-point f first-guess) | |
(define tolerance 0.00001) | |
(define (close-enough? v1 v2) | |
(< (abs (- v1 v2)) tolerance)) | |
((iterative-improve2 close-enough? f) first-guess)) | |
(define (sqrt x) | |
(fixed-point (average-damp (lambda (y) (/ x y))) | |
1.0)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (iterative-improve good-enough? improve) | |
(define (try guess) | |
(let ((next (improve guess))) | |
(if (good-enough? guess next) | |
next | |
(try next)))) | |
(lambda (first-guess) (try first-guess))) |