英文:
In SML - Why doesn't simple recursion always return 0 if first expression met?
问题
以下是您要翻译的内容:
"在一个简单的递归中,如果第一个表达式为真,那么返回0。如果在递归中的步骤一直持续,直到第一个表达式为真,为什么不总是返回0?
fun stepping (n: int, number: int) =
if number > n
then 0
else 1 + stepping (n, number + 1)
看起来函数 stepping
应该将数字递增1,直到数字大于n,然后始终返回0。相反,它返回通过递归循环的次数,直到数字大于n。
上述代码在SML中测试良好,并给我我想要的结果 - 步数递增1,直到输入的“number”大于输入的“n”。但是,手动遍历递归步骤时,似乎当递增的“number” > 输入的“n” 时应该总是返回0。我漏掉了什么?"
英文:
In a simple recursion with first if expression true then 0. if steps in the recursion keeps going until that first expression is true, why isn't 0 always returned?
fun stepping (n : int, number : int) =
if number > n
then 0
else 1 + stepping (n, number + 1)
It seems like the function stepping should add one onto number until number > n and then always return 0. Instead, it returns the number of times you went through the recursion cycle until number becomes greater than n.
The above code tests good in SML and gave me what I wanted - the number of steps incrementing by 1 until input "number" is greater than the input "n". But manually walking through the recursion steps, it seems like the return should always be 0 when the incremented "number" > the input "n". What am I missing?
答案1
得分: 1
我认为你误解了递归链中对stepping
的最终调用的结果(这将始终为零)被视为表达式返回的最终值,但事实并非如此。它实际上是构成总返回值的较大方程的一部分。
例如,如果我们看一下在评估stepping(3, 1)
时,每次递归调用都是如何构建表达式的...
result = stepping(3, 1)
result = 1 + stepping(3, 2)
result = 1 + 1 + stepping(3, 3)
result = 1 + 1 + 1 + stepping(3, 4)
result = 1 + 1 + 1 + 0
result = 3
英文:
I think you're mistaking the result of the final call to stepping
in the recursive chain (which will always be zero) as being the ultimate value returned by the expression, but that is not the case. It is actually part of a larger equation that makes up the overall returned value.
For example, if we look at how the expression gets built up as each recursive call is made when evaluating stepping(3, 1)
, you end up with...
result = stepping(3, 1)
result = 1 + stepping(3, 2)
result = 1 + 1 + stepping(3, 3)
result = 1 + 1 + 1 + stepping(3, 4)
result = 1 + 1 + 1 + 0
result = 3
答案2
得分: 1
不要有别的内容,只返回翻译好的部分:
"Let's say that I'm going to give you some money this year, according to this scheme:
- you get nothing on the first of January
- on all other days, you get one dollar more than I would have given you the day before
How much would I have to pay you today, the 20th of February?
Fifty dollars or nothing at all?
If you follow the calendar backwards, you will eventually reach January 1st, where the payment is zero, so would you expect to get nothing?
To answer your immediate question: the function does always return 0 if the first condition is met – that is, if number > n
.
However, if the first condition isn't met – number <= n
– it does not return 0 but 1 + stepping (n, number + 1)
.
It works exactly like if you called a function with a different name; that function computes a value and then this function adds 1.
It's not like returning a value from inside a loop, such as (pseudocode)
while (true)
{
if number > n
return 0
else
number = number +1
}
which is perhaps what you're thinking about."
英文:
Let's say that I'm going to give you some money this year, according to this scheme:
- you get nothing on the first of January
- on all other days, you get one dollar more than I would have given you the day before
How much would I have to pay you today, the 20th of February?
Fifty dollars or nothing at all?
If you follow the calendar backwards, you will eventually reach January 1st, where the payment is zero, so would you expect to get nothing?
To answer your immediate question: the function does always return 0 if the first condition is met – that is, if number > n
.
However, if the first condition isn't met – number <= n
– it does not return 0 but 1 + stepping (n, number + 1)
.
It works exactly like of you called a function with a different name; that function computes a value and then this function adds 1.
It's not like returning a value from inside a loop, such as (pseudocode)
while (true)
{
if number > n
return 0
else
number = number +1
}
which is perhaps what you're thinking about.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论