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

i have a scheme question

Name: Anonymous 2012-07-18 1:56

i have created a function called "repeat" that composes a function with itself n times. i have written it in two different ways. one works and the other doesn't. can you help me understand why the one doesn't work? tia.

the code:
https://gist.github.com/3134464

Name: Anonymous 2012-07-18 3:03

>>1

This is an ugly tail recursive version,


(define (repeat f n)
  (letrec ((loop (lambda (n accumulation)
                   (if (= n 0)
                     accumulation
                     (loop (- n 1) (compose f accumulation))))))
    (loop (- n 1) f)))


Repeated squaring:


(define (repeat f n)
  (cond ((= n 1) f)
        ((even? n) (repeat (compose f f) (/ n 2)))
        (else (compose f (repeat f (- n 1))))))

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