60 likes | 167 Views
(define (times1 a n) (if (= n 0) 0 (+ a (times1 a (- n 1))))) (define (times2 a n) (letrec ((iter (lambda (c result) (if (= c 0) result (iter (- c 1) (+ result a)))))) (iter n 0))).
E N D
(define (times1 a n) (if (= n 0) 0 (+ a (times1 a (- n 1))))) (define (times2 a n) (letrec ((iter (lambda (c result) (if (= c 0) result (iter (- c 1) (+ result a)))))) (iter n 0)))
(define sum-int (lambda (a b) (if (> a b) 0 (+ a (sum-int (+ a 1) b))))) (define sum-squares (lambda (a b) (if (> a b) 0 (+ (square a) (sum-squares (+ a 1) b))))) (define sum-cubes (lambda (a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))) (define (cube x) (* x x x))
(define sum (lambda (a b thing-to-sum) (if (> a b) 0 (+ (thing-to-sum a) (sum (+ a 1) b thing-to-sum))))) (define (sum-int a b) (sum a b (lambda (x) x))) (define (sum-squares a b) (sum a b square)) (define (sum-cubes a b) (sum a b cube))
(define sum (lambda (a b thing-to-sum step) (if (> a b) 0 (+ (thing-to-sum a) (sum (step a) b thing-to-sum step))))) (define (plus-one a) (+ a 1)) (define (sum-int a b) (sum a b (lambda (x) x) plus-one)) (define (sum-squares a b) (sum a b square plus-one)) (define (sum-cubes a b) (sum a b cube plus-one))
(sum 1 2 square one-plus) ==> (+ (square 1) (sum 2 2 square one-plus)) ==> (+ (square 1) (+ (square 2) (sum 3 2 square one-plus))) ==> (+ (square 1) (+ (square 2) 0)) ==> (+ 1 (+ 4 0)) ==> 5
(define (integral f a b dx) (* dx (sum a b f (lambda (x) (+ x dx))))) a dx Arctan(x) = 0 x2+1 (define (arctan a) (integral (lambda (x) (/ 1 (+ (square x) 1))) 0 a 0.001))