gpt4 book ai didi

forth - Forth 中有条件退出的说法吗?

转载 作者:行者123 更新时间:2023-12-03 16:01:02 24 4
gpt4 key购买 nike

在 Forth 中,如果栈顶为零,是否有一个常用词来有条件地退出过程(返回)?我正在考虑在递归过程中使用它而不是 IF。

最佳答案

有一个普遍实现的词叫做“?exit”,如果不是零,它就会退出。你将不得不做:

0= ?exit
去得到你想要的。如果你的 Forth 缺少这个,你可以自己定义,但是严格来说需要了解 Forth 的实现细节才能正确实现。但是,在大多数 Forth 上,以下代码将起作用:
   : ?exit if rdrop exit then ;
: ?exit if r> drop exit then ; ( if "rdrop" is not available )
: -?exit 0= if rdrop exit then ; ( what you want )
大多数 Forth 实现只有一个值用于每个函数调用,因此这将适用于其中的大多数。
还有一个更便携的版本:
: ?exit postpone if postpone exit postpone then ; immediate
: -?exit postpone 0= postpone if postpone exit postpone then ; immediate
尽管我注意到并非所有 Forth 实现都实现了“推迟”,而是可能使用“[编译]”之类的词。

关于forth - Forth 中有条件退出的说法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66266092/

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