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
> (f 1) | |
1 | |
> (f 2) | |
2 | |
> (f 3) | |
4 | |
> (f 4) | |
11 | |
> (f 5) | |
25 |
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 (f-iter n) | |
(f-iter-aux 2 1 0 n)) | |
(define (f-iter-aux a b c n) | |
(if (= 0 n) | |
c | |
(f-iter-aux (+ a (* 2 b) (* 3 c)) | |
a | |
b | |
(- n 1)))) |
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 (pascal row col) | |
(cond ((= col 1) 1) | |
((= col row) 1) | |
(else (+ (pascal (- row 1) (- col 1)) | |
(pascal (- row 1) col))))) |
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
;;;(sine 12.15) | |
(sine 12.15) | |
(p (sine 4.05)) | |
(p (p (sine 1.35))) | |
(p (p (p (sine 0.45)))) | |
(p (p (p (p (sine 0.15))))) | |
(p (p (p (p (p (sine 0.05)))))) | |
(p (p (p (p (p 0.05))))) |
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 (square x) (* x x)) | |
(define (even? n) | |
(= (remainder n 2) 0)) | |
(define (fast-expt b n) | |
(fast-expt-iter 1 b n)) | |
(define (fast-expt-iter a b n) | |
(cond ((= n 0) a) |
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 (double a) (* a 2)) | |
(define (halve a) (/ a 2)) | |
(define (even? n) (= (remainder n 2) 0)) | |
(define (fast-mult a b ) | |
(fast-mult-iter 0 a b)) | |
(define (fast-mult-iter a b n) |
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
;;; (fast-mult 3 15) | |
(fast-mult-iter 0 3 15) | |
(fast-mult-iter 3 3 14) | |
(fast-mult-iter 3 6 7) | |
(fast-mult-iter 9 6 6) | |
(fast-mult-iter 9 12 3) | |
(fast-mult-iter 21 12 2) | |
(fast-mult-iter 21 24 1) | |
(fast-mult-iter 45 24 0) | |
45 |
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
;;;(fast-expt 2 7) | |
(* 2 (fast-expt 2 6)) | |
(* 2 (square (fast-expt 2 3))) | |
(* 2 (square (* 2 (fast-expt 2 2)))) | |
(* 2 (square (* 2 (square (fast-expt 2 1))))) | |
(* 2 (square (* 2 (square (* 2 (fast-expt 2 0)))))) | |
(* 2 (square (* 2 (square (* 2 1)))) | |
(* 2 (square (* 2 (square 2)))) | |
(* 2 (square (* 2 4))) | |
(* 2 (square 8)) |
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
;;;(fast-mult 2 7) | |
(+ 2 (fast-mult 2 6)) | |
(+ 2 (* 2 (fast-mult 2 3))) | |
(+ 2 (* 2 (+ 2 (fast-mult 2 2)))) | |
(+ 2 (* 2 (+ 2 (* 2 (fast-mult 2 1))))) | |
(+ 2 (* 2 (+ 2 (* 2 (+ 2 (fast-mult 2 0)))))) | |
(+ 2 (* 2 (+ 2 (* 2 (+ 2 0))))) | |
(+ 2 (* 2 (+ 2 (* 2 2)))) | |
(+ 2 (* 2 (+ 2 4))) | |
(+ 2 (* 2 6)) |
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 (double n) (* n 2)) | |
(define (halve n) (/ n 2)) | |
(define (even? n) (= (remainder n 2) 0)) | |
(define (fast-mult a b) | |
(cond ((= b 0) 0) | |
((even? b) (double (fast-mult a (halve b)))) | |
(else (+ a (fast-mult a (- b 1)))))) |