SHARE
TWEET

Untitled

a guest Oct 21st, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (define (succ n) (+ n 1))
  2.  
  3. (define (pred n) (- n 1))
  4.  
  5. (define (add-iter a b)
  6.   (define (for i prod)
  7.     (if (= i 0)
  8.         prod
  9.         (for (pred i) (succ prod))))
  10.   (for b a))
  11.  
  12. (define (multiply-iter a b)
  13.   (define (for i prod)
  14.     (if (= i 0)
  15.         prod
  16.         (for (pred i) (add-iter prod a))))
  17.   (for b 0))
  18.  
  19. (define (fact-iter n)
  20.   (define (for i prod)
  21.     (if (= i 0)
  22.         prod
  23.         (for (pred i) (multiply-iter i prod))))
  24.   (for n 1))
  25.  
  26. (define (even? n)
  27.   (cond ((= n 1) #f)
  28.         ((= n 2) #t)
  29.         (else (even? (pred(pred n))))))
  30.  
  31. (define (div a n)
  32.   (define (for i)
  33.     (if (= (multiply-iter i a) n)
  34.         i
  35.         (for (succ i))))
  36.   (for 1))
  37.  
  38. (define (safe-div-iter n)
  39.   (if (even? n)
  40.       (div 2 n)
  41.       n))
  42.  
  43. (define (fib-iter n)
  44.   (define (for i prod pred1)
  45.     (if (= i n)
  46.         prod
  47.         (for (succ i) (add-iter pred1 prod) prod)))
  48.   (for 1 1 0))
  49.  
  50. (define (count-digits n)
  51.   (if (= (quotient n 10) 0)
  52.       1
  53.       (+ 1 (count-digits (quotient n 10)))))
  54.  
  55. (define (count-divisors n)
  56.   (define (for i prod)
  57.     (cond ((= i n) (succ prod))
  58.           ((= (remainder n i) 0) (for (succ i) (succ prod)))
  59.           (else (for (succ i) prod))))
  60.   (for 1 0))
  61.  
  62. (define (o f g) (lambda (x) (f(g x))))
  63.  
  64. (define (repeated n f x)
  65.   (if (= n 0)
  66.       x
  67.       (f (repeated (pred n) f x))))
  68.  
  69. (define (repeat n f) (lambda (x) (repeated n f x)))
  70.  
  71. (define (accumulate op start f begin end)
  72.   (if (> begin end)
  73.       start
  74.       (op (f begin) (accumulate op start f (succ begin) end))))
  75.  
  76. (define (count pred a b)
  77.   (accumulate + 0 (lambda (x) (if (pred x) 1 0)) a b))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top