2012-09-14 39 views
1

我有一個列表L =[a+b,b+c],我想將其轉換爲字符串並打印輸出a+bb+c如何將列表轉換爲Prolog中的字符串?

你能幫我把這個列表轉換成字符串嗎?我在SWI-Prolog中嘗試使用atomic_list_concat,但它給出了a+b的類型錯誤。

+1

可能是:你實際上想要更好的表示。 – false

回答

1

列表的成員是複合詞,所以你必須讓他們原子調用atomic_list_concat前:

custom_print(L) :- 
    maplist(term_to_atom, L, L1), 
    atomic_list_concat(L1, L2), 
    print(L2). 
2

在SWI-Prolog的:

?- with_output_to(atom(Atom), maplist(write, [a+b, b+c])). 
Atom = 'a+bb+c'. 

您可以用電話代替write如果您需要更多控制您的術語(例如a+b)的書寫方式,請使用自定義謂詞。