如何将以下内容转换为尾递归过程? [英] How to translate the following into a tail recursive procedure?

查看:77
本文介绍了如何将以下内容转换为尾递归过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数学表达式:

; f(n) = f(n - 1) + f(n - 2)   where n >= 2 
; f(n) = n where n < 2`

我将其翻译为普通的递归LISP调用:

Which I translated into a normal recursive LISP call:

(define (f n)
  (cond ((< n 2) n)
        (else (+ (f (- n 1))
                 (f (- n 2))))))

我如何将以上内容转换为尾递归程序?我不习惯函数式编程,所以我有点挣扎.

How would I translate the above into a tail-recursive procedure? I'm not used to functional programming so I'm struggling a bit.

推荐答案

您正在谈论建立尾部递归变换以计算斐波那契数的示例.您可以在 SICP的本章.

You are talking about established example of tail recursive transformation for computing Fibonacci numbers. You can find excellent description with code samples at this chapter of SICP.

这篇关于如何将以下内容转换为尾递归过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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