2016-03-15 36 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

相關問題