1 / 6

(define (times1 a n) (if (= n 0) 0 (+ a (times1 a (- n 1)))))

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

nike
Download Presentation

(define (times1 a n) (if (= n 0) 0 (+ a (times1 a (- n 1)))))

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. (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

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

More Related