为什么每个人在单词定义中的行为都不同?
Why does each behave differently inside word definitions?
为数组中的 each
做一个引用:
(scratchpad) { "3.1415" "4" } [ string>number ] each
3.1415
4
要在一个字内做到这一点:
(scratchpad) : conveach ( x -- y z ) [ string>number ] each ;
(scratchpad) { "3.1415" "4" } conveach .
但这会引发错误:
The word conveach cannot be executed because it failed to compile
The input quotation to “each” doesn't match its expected effect
Input Expected Got
[ string>number ] ( ... x -- ... ) ( x -- x )
我做错了什么?
Factor 要求所有单词都具有已知的堆叠效果。编译器想知道这个词从堆栈中吃掉了多少项,又放回了多少项。在侦听器中,您输入的代码没有该限制。
{ "3.1415" "4" } [ string>number ] each
不从堆栈中取出任何项目,但将两个项目放在那里。堆栈效应将表示为 ( -- x y )
。
[ string>number ] each
另一方面,此代码获取一个项目,但将 0 到许多项目放回堆栈中。数字 变化 取决于给定每个序列的长度。
! ( x -- x y z w )
{ "3" "4" "5" "6" } [ string>number ] each
! ( x -- )
{ } [ string>number ] each
! ( x -- x )
{ "2" } [ string>number ] each
您可能想改用 map
词,这会将您的序列从包含字符串的序列转换为包含数字的序列。像这样:
: convseq ( strings -- numbers ) [ string>number ] map ;
IN: scratchpad { "3" "4" } convseq .
{ 3 4 }
为数组中的 each
做一个引用:
(scratchpad) { "3.1415" "4" } [ string>number ] each
3.1415
4
要在一个字内做到这一点:
(scratchpad) : conveach ( x -- y z ) [ string>number ] each ;
(scratchpad) { "3.1415" "4" } conveach .
但这会引发错误:
The word conveach cannot be executed because it failed to compile
The input quotation to “each” doesn't match its expected effect
Input Expected Got
[ string>number ] ( ... x -- ... ) ( x -- x )
我做错了什么?
Factor 要求所有单词都具有已知的堆叠效果。编译器想知道这个词从堆栈中吃掉了多少项,又放回了多少项。在侦听器中,您输入的代码没有该限制。
{ "3.1415" "4" } [ string>number ] each
不从堆栈中取出任何项目,但将两个项目放在那里。堆栈效应将表示为 ( -- x y )
。
[ string>number ] each
另一方面,此代码获取一个项目,但将 0 到许多项目放回堆栈中。数字 变化 取决于给定每个序列的长度。
! ( x -- x y z w )
{ "3" "4" "5" "6" } [ string>number ] each
! ( x -- )
{ } [ string>number ] each
! ( x -- x )
{ "2" } [ string>number ] each
您可能想改用 map
词,这会将您的序列从包含字符串的序列转换为包含数字的序列。像这样:
: convseq ( strings -- numbers ) [ string>number ] map ;
IN: scratchpad { "3" "4" } convseq .
{ 3 4 }