递归循环中的值
在递归循环中,我想更改变量的值:
loop(N) when N > ... ->
N;
loop(N) ->
case ... of
N+1
...
end,
...
case ... of
N-1
...
end,
...
loop(N).
如何“传递” N 的新值?
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).
How to "pass" the new value of N ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
N
的值一旦分配就无法更改,因此需要使用新值调用loop
:或者,在传递之前创建一个临时变量来保存其新值它进入递归
循环
调用。如果这会导致代码中出现大量重复,您可能需要将循环构造与生成 N 新值的逻辑分开:
Since you can't change the value of
N
once assigned, you need to callloop
with the new value:Alternatively, create a temporary variable to hold its new value before passing it into the recursive
loop
invocation.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:
您只需使用新值调用该函数即可:
无耻地窃取自: http://learnyousomeerlang.com/recursion#hello -递归
You simply call the function with a new value:
Shamelessly stolen from: http://learnyousomeerlang.com/recursion#hello-recursion