>>10
both are correct, but the times at which the closures are evaluated will be different. Say you (define fn (repeat f n)). If repeat was defined as:
(define (repeat f n)
(if (> n 1)
(let ((g (repeat f (- n 1))))
(lambda (x)
(f (g x))))
f))
then all the composition closures are constructed and linked together when you called (repeat f n). When (fn x) is called, the constant linked data structure of closures is then traversed and applied to x and each other's return values.
If repeat was instead defined as:
(define (repeat f n)
(if (> n 1)
(lambda (x)
(f ((repeat f (- n 1)) x))))
f))
Then the call to
(repeat f n) would return immediately in O(1) time. But then every time (fn x) was evaluated, then chain of composed closures would be recreated, over and over again.
But the two forms are ultimately equivalent. They just might differ in efficiency.