function - 重复应用Scheme中的函数?

标签 function scheme

If f is a numerical function and n is a positive integer, then we can form the nth repeated application of f, which is defined to be the function whose value at x is f(f(...(f(x))...)). For example, if f is the function x + 1, then the nth repeated application of f is the function x + n. If f is the operation of squaring a number, then the nth repeated application of f is the function that raises its argument to the 2^nth power. 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. Your procedure should be able to be used as follows:

((repeated square 2) 5)
625

You can use this to simplify the answer:

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

最佳答案

(define (repeated f n)
  (if (= n 1)
      f
      (compose f (repeated f (- n 1)))))

关于function - 重复应用Scheme中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/249557/

相关文章:

php - 非常简单的 SQL 函数返回 bool 值 1 (true) 而不是资源

scheme - 递归函数计算从1到n的所有数字的总和?

scheme - GNU 方案输出

lisp - Scheme 中的函数不能返回任何内容吗?

clojure - 包 vs 命名空间 vs 模块

r - 在R函数中,将数据框对象指定为名称?循环遍历函数

bash - 如何从函数内部确定函数名称

c - 如何以非 super 用户身份安装 postgres 的 tablefunc

Javascript:如何在函数内无延迟地执行函数?

c# - 为什么 C# 没有词法嵌套函数?