Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define (succ n) (+ n 1))
- (define (pred n) (- n 1))
- (define (add-iter a b)
- (define (for i prod)
- (if (= i 0)
- prod
- (for (pred i) (succ prod))))
- (for b a))
- (define (multiply-iter a b)
- (define (for i prod)
- (if (= i 0)
- prod
- (for (pred i) (add-iter prod a))))
- (for b 0))
- (define (fact-iter n)
- (define (for i prod)
- (if (= i 0)
- prod
- (for (pred i) (multiply-iter i prod))))
- (for n 1))
- (define (even? n)
- (cond ((= n 1) #f)
- ((= n 2) #t)
- (else (even? (pred(pred n))))))
- (define (div a n)
- (define (for i)
- (if (= (multiply-iter i a) n)
- i
- (for (succ i))))
- (for 1))
- (define (safe-div-iter n)
- (if (even? n)
- (div 2 n)
- n))
- (define (fib-iter n)
- (define (for i prod pred1)
- (if (= i n)
- prod
- (for (succ i) (add-iter pred1 prod) prod)))
- (for 1 1 0))
- (define (count-digits n)
- (if (= (quotient n 10) 0)
- 1
- (+ 1 (count-digits (quotient n 10)))))
- (define (count-divisors n)
- (define (for i prod)
- (cond ((= i n) (succ prod))
- ((= (remainder n i) 0) (for (succ i) (succ prod)))
- (else (for (succ i) prod))))
- (for 1 0))
- (define (o f g) (lambda (x) (f(g x))))
- (define (repeated n f x)
- (if (= n 0)
- x
- (f (repeated (pred n) f x))))
- (define (repeat n f) (lambda (x) (repeated n f x)))
- (define (accumulate op start f begin end)
- (if (> begin end)
- start
- (op (f begin) (accumulate op start f (succ begin) end))))
- (define (count pred a b)
- (accumulate + 0 (lambda (x) (if (pred x) 1 0)) a b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement