gpt4 book ai didi

function - 如何在满足 if 条件时重新启动函数

转载 作者:行者123 更新时间:2023-12-04 14:53:54 24 4
gpt4 key购买 nike

如何在满足或不满足 if 条件时“重启”我的函数。
在此伪代码中,如果满足 if 条件,我想停止函数正在执行的操作并重新启动。

function example()

.
.
.

for i in 1:N
for j in 1:N
if Mat[i,j] > 1
Mat[i,j] += restart? # Here the process should restart,
end
end
end

最佳答案

返回值不是添加额外的分支并使函数复杂化,而是为 I.M.H.O. 中的控制流提供了强大的能力。更清洁的时尚。添加一点单独的功能很简单

function do_thing_once!(mat)
# Returns true if successful
for i in 1:N
for j in 1:N
if mat[i,j] > 1
mat[i,j] += 123
return false
end
end
end
return true
end

function do_all_things!(mat)
success = false
while !success
success = do_thing_once!(mat)
end
end

关于function - 如何在满足 if 条件时重新启动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68511596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com