斐波那契计划 [英] Fibonacci in Scheme
问题描述
我试图理解Scheme中的递归,因此很难进行空运行,例如一个简单的斐波那契数问题.
I am trying to understand recursion in Scheme and I have a hard time doing the dry run for it, for example a simple Fibonacci number problem.
有人可以替我分解添加步骤吗?
Could someone break down the steps in which the additions take place, for me?
(define (fib n)
(if (<= n 2)
1
(+ (fib (- n 1)) (fib (- n 2)))))
推荐答案
如果您使用的是Racket(如标签所示),则说明您有内置的步进器.
If you're using Racket, as your tags indicate, then you have a built-in stepper.
将程序输入DrRacket,然后单击右上角菜单中的步骤":
Enter the program into DrRacket, and click Step in the top-right menu:
然后将打开一个步进器窗口.单击一遍又一遍",您可以逐步完成程序的执行.
Then a stepper window will open up. Click Step over and over, and you can walk through the execution of the program.
如果您希望步数更易于管理,请选择一个小于10的数字以跟踪执行.
If you want the number of steps to be a bit more manageable, pick a number lower than 10 for the execution to trace.
这篇关于斐波那契计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!