我如何更清楚地表达这个Scheme函数?

时间:2022-10-10 18:35:08
(define (repeated f n)
   if (= n 0)
   f
   ((compose repeated f) (lambda (x) (- n 1))))

I wrote this function, but how would I express this more clearly, using simple recursion with repeated?

我写了这个函数,但是如何使用重复的简单递归更清楚地表达这个?

I'm sorry, I forgot to define my compose function.

对不起,我忘了定义我的撰写功能。

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

And the function 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.

并且该函数将计算f和正整数n的过程作为输入,并返回计算f的第n次重复应用的过程。

3 个解决方案

#1


1  

I'm assuming that (repeated f 3) should return a function g(x)=f(f(f(x))). If that's not what you want, please clarify. Anyways, that definition of repeated can be written as follows:

我假设(重复f 3)应该返回一个函数g(x)= f(f(f(x)))。如果这不是你想要的,请澄清。无论如何,重复的定义可以写成如下:

(define (repeated f n)
  (lambda (x)
    (if (= n 0)
        x
        ((repeated f (- n 1)) (f x))))) 

(define (square x)
  (* x x))

(define y (repeated square 3))

(y 2) ; returns 256, which is (square (square (square 2)))

#2


1  

(define (repeated f n)
  (lambda (x)
    (let recur ((x x) (n n))
      (if (= n 0)
          args
          (recur (f x) (sub1 n))))))

Write the function the way you normally would, except that the arguments are passed in two stages. It might be even clearer to define repeated this way:

以通常的方式编写函数,除了参数分两个阶段传递。以这种方式重复定义可能更清楚:

(define repeated (lambda (f n) (lambda (x) 
  (define (recur x n)
    (if (= n 0)
        x
        (recur (f x) (sub1 n))))
  (recur x n))))

You don't have to use a 'let-loop' this way, and the lambdas make it obvious that you expect your arguments in two stages. (Note:recur is not built in to Scheme as it is in Clojure, I just like the name)

你不必以这种方式使用'let-loop',而lambdas显然你希望你的论证分两个阶段。 (注意:recur不是内置于Scheme中,因为它在Clojure中,我就像这个名字一样)

> (define foonly (repeat sub1 10))
> (foonly 11)
1
> (foonly 9)
-1

The cool functional feature you want here is currying, not composition. Here's the Haskell with implicit currying:

你想要的很酷的功能是currying,而不是组合。这是带有隐式currying的Haskell:

repeated _ 0 x = x
repeated f n x = repeated f (pred n) (f x)

I hope this isn't a homework problem.

我希望这不是一个功课问题。

#3


0  

What is your function trying to do, just out of curiosity? Is it to run f, n times? If so, you can do this.

出于好奇,你的功能是什么?它是运行f,n次吗?如果是这样,你可以这样做。

(define (repeated f n)
  (for-each (lambda (i) (f)) (iota n)))

#1


1  

I'm assuming that (repeated f 3) should return a function g(x)=f(f(f(x))). If that's not what you want, please clarify. Anyways, that definition of repeated can be written as follows:

我假设(重复f 3)应该返回一个函数g(x)= f(f(f(x)))。如果这不是你想要的,请澄清。无论如何,重复的定义可以写成如下:

(define (repeated f n)
  (lambda (x)
    (if (= n 0)
        x
        ((repeated f (- n 1)) (f x))))) 

(define (square x)
  (* x x))

(define y (repeated square 3))

(y 2) ; returns 256, which is (square (square (square 2)))

#2


1  

(define (repeated f n)
  (lambda (x)
    (let recur ((x x) (n n))
      (if (= n 0)
          args
          (recur (f x) (sub1 n))))))

Write the function the way you normally would, except that the arguments are passed in two stages. It might be even clearer to define repeated this way:

以通常的方式编写函数,除了参数分两个阶段传递。以这种方式重复定义可能更清楚:

(define repeated (lambda (f n) (lambda (x) 
  (define (recur x n)
    (if (= n 0)
        x
        (recur (f x) (sub1 n))))
  (recur x n))))

You don't have to use a 'let-loop' this way, and the lambdas make it obvious that you expect your arguments in two stages. (Note:recur is not built in to Scheme as it is in Clojure, I just like the name)

你不必以这种方式使用'let-loop',而lambdas显然你希望你的论证分两个阶段。 (注意:recur不是内置于Scheme中,因为它在Clojure中,我就像这个名字一样)

> (define foonly (repeat sub1 10))
> (foonly 11)
1
> (foonly 9)
-1

The cool functional feature you want here is currying, not composition. Here's the Haskell with implicit currying:

你想要的很酷的功能是currying,而不是组合。这是带有隐式currying的Haskell:

repeated _ 0 x = x
repeated f n x = repeated f (pred n) (f x)

I hope this isn't a homework problem.

我希望这不是一个功课问题。

#3


0  

What is your function trying to do, just out of curiosity? Is it to run f, n times? If so, you can do this.

出于好奇,你的功能是什么?它是运行f,n次吗?如果是这样,你可以这样做。

(define (repeated f n)
  (for-each (lambda (i) (f)) (iota n)))