2016-03-15 37 views
0

要在阵列做一个报价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) 

我在做什么错误?

回答

1

因子要求所有单词具有已知的叠加效果。编译器想知道这个词从栈中吃掉了多少个项目以及它放回的数量。在监听器中,您输入的代码不具有该限制。

{ "3.1415" "4" } [ string>number ] each 

从堆栈中取出任何物品,但将其中放置两个。堆栈效应将被表示为(-- x y)

[ string>number ] each 

另一方面,这段代码需要一个项目,但是将多个项目放回到栈中。数字各不相同取决于给予每个序列的时间长度。

! (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 } 
+0

是的,我知道我可以使用地图,但我选择每个原因也在于此:http://stackoverflow.com/questions/36047593/unpack-an-array-to-the-堆栈在运行时 – cat

相关问题