Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[SICP] Ex 1.30 / 1.31

Name: Anonymous 2008-02-10 7:46

Here's what I have

; Ex 1.30
(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))
; Ex 1.31
(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* result (term a)))))
  (iter a 1))

(define (fac n)
  (product (lambda (x) x) 1 (lambda (x) (+ x 1)) n))


Am i rite?

Name: Anonymous 2008-02-12 6:37

OP again, exercise 1.42 & 1.43
Here is what I have

; Ex 1.42
; composes an arbitrary number of procedures

(define (compose . l)
  (lambda (x)
    (define (compose-iter l x)
       (if (null? l) x
           (compose-iter (cdr l) ((car l) x))))
     (compose-iter (reverse l) x)))


Works nicely.

As for exercise 1.43..


; Does scheme have such function? there is a make-string but I cannot find equivalent for lists

(define (make-list x n)
  (define (iter l n)
    (if (= n 0) l
        (iter (cons x l) (- n 1))))
  (iter '() n))

(define (repeated f n)
  (apply compose (make-list f n)))


What do you think?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List