Forth 中有条件退出这个词吗?

Is there a word for a conditional exit in Forth?

在 Forth 中,是否有一个常用词来在栈顶为零时有条件地退出过程 (return)?我正在考虑在递归过程中使用它而不是 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 实现都实现了“推迟”,并且可能会使用像“[编译]”这样的词。

便携式实现:

: ?exit ( x -- ) postpone if postpone exit postpone then ; immediate
: 0?exit ( x -- ) postpone 0= postpone ?exit ; immediate

此实现适用于任何标准 Forth 系统。