递归循环中的值 [英] value in a recursive loop
本文介绍了递归循环中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在递归循环中,我想更改变量的值:
In a recursive loop, I would like to change the value of a variable:
loop(N) when N > ... ->
N;
loop(N) ->
case ... of
N+1
...
end,
...
case ... of
N-1
...
end,
...
loop(N).
如何传递N的新值?
推荐答案
由于您无法更改 N
的值,因此您需要调用循环
新值:
Since you can't change the value of N
once assigned, you need to call loop
with the new value:
loop(N) ->
loop(N+1).
或者,创建一个临时变量来保存其新值,然后将其传递给递归循环
调用。
Alternatively, create a temporary variable to hold its new value before passing it into the recursive loop
invocation.
loop(N) ->
NewN = N+1,
loop(NewN).
如果这在代码中导致了很多重复,您可能需要将循环结构与产生新值N的逻辑:
If this results in a lot of repetition in your code, you might want to separate the looping construct from the logic which produces the new value of N:
loop(N) ->
NewN = logic(N),
loop(NewN).
logic(N) ->
N+1.
这篇关于递归循环中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文