我試圖寫一個簡單的翻譯,在那裏你輸入數字號碼的清單,並返回類似這樣的字符串值:我的Prolog的「轉換器」腳本返回一個空列表
翻譯([1,2, 3],X)。 X = [一,二,三]。
我寫的代碼作品...除了它返回一個空的列表。這裏是我的代碼和跟蹤:
means(1, one).
means(2, two).
means(3, three).
means(4, four).
means(5, five).
means(6, six).
means(7, seven).
means(8, eight).
means(9, nine).
means(10, ten).
translate([], X).
translate([H|T], []):-
means(H, X),
translate(T, X).
translate([H|T], X):-
means(H, Y),
translate(T, [X|Y]).
[trace] 1 ?- translate([1,2,3], X).
Call: (6) translate([1, 2, 3], _G2219) ? creep
Call: (7) means(1, _G2301) ? creep
Exit: (7) means(1, one) ? creep
Call: (7) translate([2, 3], one) ? creep
Call: (8) means(2, _G2301) ? creep
Exit: (8) means(2, two) ? creep
Call: (8) translate([3], [one|two]) ? creep
Call: (9) means(3, _G2304) ? creep
Exit: (9) means(3, three) ? creep
Call: (9) translate([], [[one|two]|three]) ? creep
Exit: (9) translate([], [[one|two]|three]) ? creep
Exit: (8) translate([3], [one|two]) ? creep
Exit: (7) translate([2, 3], one) ? creep
Exit: (6) translate([1, 2, 3], []) ? creep
X = [] .
我的另一個問題是:爲什麼我的列表作爲串聯[一個|二] |三]?而不是[一,二,三]?
感謝,
彼得,你有任何改變想要刪除這個問題嗎?如果是這樣,請出於定製原因舉報主持人。我回滾了你的編輯,因爲它可以被認爲是針對你自己的帖子的破壞行爲。 – bfavaretto 2013-02-28 03:29:17