计算函数第n次重复应用的方案过程? [英] Scheme procedure to compute the nth repeated application of a function?

查看:53
本文介绍了计算函数第n次重复应用的方案过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人熟悉吗?

编写一个过程作为输入计算f和a的过程正整数n并返回计算第n个过程f.重复应用.这该程序应该能够用作如下:

Write a procedure that takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the nth repeated application of f. The procedure should be able to be used as follows:

((repeated square 2) 5)
625

我知道我为函数的组成而创建的以下代码将有助于简化解决方案,但是我不确定从何而来:

I know that the following code I've created for the composition of functions will help make the solution simpler, but I'm not sure where to go from here:

(define (compose f g) (lambda (x) (f (g x))))

推荐答案

好吧,您可能想要这样的东西,对吧?

Well, you probably want something like 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))))

(我不知道Scheme中是否预定义了 identity .如果没有,则很容易编写.)

(I don't know whether identity is predefined in Scheme. If not, it's easy to write.)

现在,这不是直接可复制的,因为您不能在调用 repeated 的代码之外用任意的东西神奇地封装代码.但是,使用 compose 进行重写时,这些简化步骤是什么样的?您能在结果步骤列表中找出一个模式并进行重现吗?

Now, this is not directly reproducible because you can't magically enclose code outside of the call to repeated with arbitrary stuff. However, what do these reduction steps look like when rewritten using compose? Can you make out a pattern in the resulting list of steps and reproduce it?

这篇关于计算函数第n次重复应用的方案过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆