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

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -