2013-12-14 146 views
0

有沒有辦法讓portray_clause寫入變量而不是標準輸出?portray_clause變量在Prolog

?- portray_clause(f(x):app(X, Y, Z)). 
    f(x):app(_, _, _). 
    true. 

?- portray_clause(Output, f(x):-app(X, Y, Z)). 
    ERROR: Arguments are not sufficiently instantiated 

預先感謝您

+0

你需要實現什麼?打印到另一個流(與標準輸出不同)或存儲表示子句的結構? – rano

+0

我需要存儲一個表示portray_clause的「漂亮的輸出」的結構。因此,如果它打印「f(x):app(_,_,_)」。我需要Ouput = f(x):app(_,_,_)。 –

+0

Prolog是homoiconic,如果您可以將您的子句傳遞給portray_clause謂詞,則可以將其存儲在其他位置 – rano

回答

3

如果您正在使用SWI-Prolog的,你可以使用with_output_to/2。例如,要寫一個原子,你會說:

?- with_output_to(atom(A), portray_clause(foo(X) :- bar)). 
A = 'foo(_) :-\n\tbar.\n'. 
+0

非常感謝! –

+0

@MatthiasVanEeghem您應該標記您的問題[swi-prolog],因爲這是SWI-Prolog擴展。 – 2013-12-14 17:44:35

+0

好的,會做:) –