Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. (define (product term a next b)
  2. (cond
  3. ((> a b) 1)
  4. (else (* (term a) (product term (next a) next b)))))
  5.  
  6. (define (i-product term a next b)
  7. (cond ((> a b) null)
  8. (else
  9. (define (iter a result)
  10. (cond ((> a b) result)
  11. (else (iter (next a) (* (term a) result)))))
  12. (iter a 1))))
  13.  
  14. (define (identity x) x)
  15. (define (inc x) (+ 1 x))
  16. (define (multiply-integers a b) (i-product identity a inc b))
  17.  
  18. (define (square x) (* x x))
  19. (define (compute-pi steps)
  20. (define (next n) (+ n 2.0))
  21. (* 8.0 (* steps 2) (/ (i-product square 4.0 next (* (- steps 1) 2))
  22. (i-product square 3.0 next (* steps 2)))))
  23.  
  24. (define (factorial n)
  25. (define (next n) (+ n 1))
  26. (i-product identity 1 next n))
  27.  
  28. (define (product term a next b)
  29. (if (> a b)
  30. 1
  31. (* (term a) (product term (next a) next b))))
  32.  
  33. (define (i-product term a next b)
  34. (define (iter a result)
  35. (if (> a b)
  36. result
  37. (iter (next a) (* (term a) result))))
  38. (iter a 1))
  39.  
  40. (define (compute-pi steps)
  41. (define (next n) (+ n 2))
  42. (* 8.0 (* steps 2) (/ (i-product square 4 next (* (- steps 1) 2))
  43. (i-product square 3 next (* steps 2)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement