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:51

>>10
it's not.

change from this
(define (repeat f n)
        (if (> n1)
            (lambda (x) (f (repeat f (- n 1))))
            f))
to this:
(define ...
        (if ...
            (lambda (x) (f ((repeat f (- n 1)) x)))
            ...))

the difference is in when the recursive call is evaluated. putting the repeat in the lambda causes it to be evaluated every time the function is called. you're working with higher order functions so its fairly strange you would want to embed a recursive call to the lambda creating a lambda into the lambda that's being created.

also you should define the base case as values or identity.

a simple definition:
(define (repeat n proc)
  (foldl compose values (make-list n proc)))

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