2017-02-17 21 views
0

我在探索Prolog中的寫謂詞,但它在某些時候表現有所不同。我確實通過了some posts,但我無法發現這個問題。在Prolog中使用寫謂詞時出錯

謂:

explore_write_predicate(InputList,Index):- 
    TempIndex is Index+1, 
    select(Element,InputList,TempList), 
    write(TempIndex). 

查詢:

explore_write_predicate([1,2,3,4],1). 

結果:

2 
true 

上面的代碼工作正常,但是當我增加一個參數寫謂詞()它給出了一個錯誤。

謂:

explore_write_predicate(InputList,Index):- 
    TempIndex is Index+1, 
    select(Element,InputList,TempList), 
    write(TempIndex,Element). 

查詢:

explore_write_predicate([1,2,3,4],1). 

錯誤:

No permission to call sandboxed `write(_1132,_1134)' 
Reachable from: 
     explore_write_predicate(A,B) 
     swish_trace:swish_call(explore_write_predicate([1,2,3,4],1)) 
     '$swish wrapper'(explore_write_predicate([1,2,3,4],1),A) 

請幫我爲什麼這種不正常現象。 P.S我也看到了寫文檔,但無法從中得到很多。 任何幫助,不勝感激。

+0

寫入只有一個arity。您需要將其稱爲'write([TempIndex,Element])'使其工作。 – Enigmativity

+0

或'write(TempIndex = Element)' – CapelliC

+0

無論如何:認真考慮用'writeq'代替! – false

回答

0

你的錯誤有兩個方面:

首先,你似乎是使用SWISH爲您所選擇的解釋器,鎖定遠一些的輸入/輸出方法(包括tab/1write/2get_single_char/1put/1等),所以你將無法使用它們。

其次,write/2需要一個流作爲其第一個參數,第二個參數將被寫入 - 如註釋中所述,寫入多個東西或者將項目作爲列表傳遞,或者使用多個寫入。

+0

非常感謝。 –