Scheme procedure to compute the nth repeated application of a function? -
is familiar this?
write procedure takes inputs procedure computes f , positive integer n , returns procedure computes nth repeated application of f. procedure should able used follows:
((repeated square 2) 5) 625
i know following code i've created composition of functions make solution simpler, i'm not sure go here:
(define (compose f g) (lambda (x) (f (g x))))
well, want this, right?
((repeated square 3) 5) -> (square ((repeated square 2) 5)) -> (square (square ((repeated square 1) 5))) -> (square (square (square ((repeated square 0) 5)))) -> (square (square (square (identity 5))))
(i don't know whether identity
predefined in scheme. if not, it's easy write.)
now, not directly reproducible because can't magically enclose code outside of call repeated
arbitrary stuff. however, these reduction steps when rewritten using compose
? can make out pattern in resulting list of steps , reproduce it?
Comments
Post a Comment