简体   繁体   English

方案R5RS是递归还是迭代?

[英]Scheme R5RS, recursion or iterative?

I have programmed a scheme code with 2 functions that plusses 1 to the argument, and subtracts 1 from the argument. 我用2个函数编写了一个方案代码,该函数将参数加1,并从参数中减去1。 I've now programmed a function to do this automatically, but I don't know if it's recursive or iterative. 我现在已经编程了一个函数来自动执行此操作,但是我不知道它是递归还是迭代的。 Here's the whole code: 这是整个代码:

(define add1
     
  (lambda (x)
      
    (+ x 1)))

(define sub1
     
  (lambda (x)
    
    (- x 1)))

(define plus
    
  (lambda (x y)
    
    (if (zero? y)
    
        x
    
        (plus (add1 x) (sub1 y)))))

Now my question is, what kind of function is plus? 现在我的问题是,什么样的功能是加的? Recursive, or iterative, and why? 递归还是迭代,为什么?

Syntactically, the plus function is recursive: clearly, it's calling itself. 从语法上讲, plus函数是递归的:显然,它是在调用自身。 The interesting question is: what kind of process does it generate? 有趣的问题是:它生成什么样的过程 Given that it was written in a tail-recursive fashion (intuitively: there's nothing left to do after the recursive call), we can state that the process it generates is iterative. 鉴于它是以尾部递归的方式编写的(直觉上:在递归调用之后没有任何事情要做),我们可以说它生成的过程是迭代的。 For a more detailed discussion of procedures and the processes they generate, refer to SICP . 有关程序及其生成过程的更详细讨论,请参阅SICP

As code goes it is a recursive procedure. 随着代码的进行,它是一个递归过程。 The process however is iterative since it does the recursion in tail position. 但是,该过程是迭代的,因为它在尾部位置进行递归。

In Scheme you can write both iterative and recursive processes with recursive functions. 在Scheme中,您可以使用递归函数编写迭代过程和递归过程。

Example: 例:

;; iterative process
;; recursive (inner) procedure
(define (fib n)
  (define (fib n a b)
    (if (zero? n)
        a
        (fib (- n 1) b (+ a b)))
  (fib n 0 1))

;; recursive process
;; recursive procedure
(define (fib n)
  (if (< n 2)
      n
      (+ (fib (- n 1))
         (fib (- n 2)))))

You can also write both recursive and iterative processes with loop constructs, though in Scheme such loop constructs are really just syntactic sugar for tail recursion. 您也可以使用循环构造编写递归和迭代过程,尽管在Scheme中,此类循环构造实际上只是尾递归的语法糖。 It's fairly common in other languages though in order to be able to recurse deeper. 为了能够更深入地递归,它在其他语言中相当普遍。

Example: 例:

;; iterative process
;; iterative procedure  (do)
(define (fib n)
  (do ((n n (- n 1))
       (a 0 b)
       (b 1 (+ a b)))
      ((zero? n) a)))

;; recursive process
;; iterative procedure (do)
(define (fib n)
  (let ((work (list n #f)))
    ;; push x-1 and x-2 onto work
    (define (push-reduce! x)
      (set! work 
            (cons (- x 1) 
                  (cons (- x 2) work))))
    ;; pop top of work
    (define (pop)
      (let ((v (car work)))
        (set! work (cdr work))
        v))

    ;; find fibonacci with a iterative
    ;; (and ugly) do loop
    (do ((n 0 (if (< c 2) (+ n c) (begin (push-reduce! c) n)))
         (c (pop) (pop)))
      ((not c) n))))

The last one is simply horrible Scheme as should be avoided, but it's pretty much used to avoid stack overflows in other languages. 最后一个是可怕的方案,应该避免,但是它非常用于避免其他语言中的堆栈溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM