PasteRack.org
Paste # 47193
2017-09-16 09:29:21

Fork as a new paste.

Paste viewed 52 times.


Embed:

exercise 1.31

#lang SICP


;; Iterative product procedure

(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a)
              (* (term a) result))))
  (iter a 1))

(define (identity x) x)

(define (inc n) (+ n 1))

(define (factorial n)
  (define (fac a b)
    (product identity a inc b))
  (fac 1 n))

(factorial 5)
(factorial 6)
(factorial 7)

(define (square x)
  (* x x))

;; Pi aproximation

(define (pi-aproximation n)
  (define (term a)
    (cond ((even? a)
        (square a))
        (else (/ 1.0 (square a)))))
  (/ (* 2.0  (product term 1 inc n)) n))

(pi-aproximation 1000000.0)
        
  

=>

standard-module-name-resolver: collection not found
  for module path: (submod SICP reader)
  collection: "SICP"
  in collection directories:
   /home/pasterack/.racket/6.8/collects
   /home/pasterack/racket68/collects
   ... [161 additional linked and package directories]
  context...:
   show-collection-err
   standard-module-name-resolver
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:112:0: get-tokens
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:56:0: typeset-code15
   /home/pasterack/pasterack/tmp/47193/47193code.scrbl: [running body]
   loop
   ...cket/cmdline.rkt:179:51
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/run.rkt: [running body]