2012-07-03 33 views
0

我在Prolog中遇到了一些簡單程序的問題。我有兩個不同的羣體,我想將一個羣組的元素附加到另一個羣組,而不直接修改事實(例如:Toronto = USA)。Prolog中不同變量的MIX值

country(usa, northamerica). 
country(canada, northamerica). 

city​​(chicago, usa). 
city​​(newyork, usa). 
city​​(losangeles, usa). 
city​​(dallas,  usa). 
city​​(miami,  usa). 
city​​(lasvegas, usa). 
city​​(seattle, usa). 

city​​(toronto, canada). 
city​​(vancouver, canada). 
city​​(ottawa,  canada). 
city​​(richmond, canada). 
city​​(winnipeg, canada). 
city​​(edmundston, canada). 
city​​(hamilton, canada). 

trip(john, usa). 
trip(jack, canada). 

在這個例子中,約翰前往美國七個城市,而傑克前往其他七個城市在加拿大。

但是,約翰最近前往多倫多。我想達到以下結果:

? - trip_plus(X, john). 

X = chicago; 
X = newyork; 
X = losangeles; 
X = dallas; 
X = miami; 
X = lasvegas; 
X = seattle; 
X = toronto; 

?- yes 

我試了很多次都沒有成功得到上面的結果。我可以得到最接近的是使用以下內容:

country(C). 
city(Y). 
trip(T). 
trip_plus(X, T) :- city(Y, C), trip(T, C). 

我在做什麼錯了?

謝謝配偶。

+0

什麼是謂詞trip/1?它在你當前的代碼中沒有提到。 –

+0

在mey問題上有一個小的錯字,* trip_plus(X,john),固定。然而。這不是問題,多倫多仍然不包括在內。 :( 謝謝。 –

回答

1

我不確定我是否正確理解了你:你想創建一個謂詞,列出所有有人訪問過的城市?我會將我的解決方案分爲三種情況。

首先,我們將列出所有直接「到城市旅行」,例如, trip(john, toronto)trip(john, newyork)

trip_plus(City, Who) :- 
    % we grab all the trip(*, *) pairs 
    trip(Who, City), 
    % and we leave only cities 
    city(City, _). 

然後,我們要列出「鄉村旅行」中的所有城市,例如, trip(john, usa)

trip_plus(City, Who) :- 
    % again, we grab all the trip(*, *) pairs 
    trip(Who, Country), 
    % and we list all cities in given country (if it is a country) 
    city(City, Country). 

,並在結束時,我們要列出所有城市中的「大陸之旅」,例如trip(jack, northamerica)

trip_plus(City, Who) :- 
    % the same thing 
    trip(Who, Continent), 
    % we list all countries on the continent 
    country(Country, Continent), 
    % and we list all cities in given country 
    city(City, Country). 

整個謂語看起來像這樣:

trip_plus(City, Who) :- 
    trip(Who, City), 
    city(City, _). 
trip_plus(City, Who) :- 
    trip(Who, Country), 
    city(City, Country). 
trip_plus(City, Who) :- 
    trip(Who, Continent), 
    country(Country, Continent), 
    city(City, Country). 

因此,對於你的世界的數據庫和這些旅行:

trip(john, usa). 
trip(jack, canada). 
trip(john, toronto). 

我們得到:

?- trip_plus(X, john). 
X = toronto ; 
X = chicago ; 
X = newyork ; 
X = losangeles ; 
X = dallas ; 
X = miami ; 
X = lasvegas ; 
X = seattle ; 
false. 

你可能會注意到toronto是第一個,而條目trip(john, toronto)是數據庫中的最後一個。這是因爲我們首先尋找「到城市」旅行。如果爲了此事確實給你,謂詞可以寫成這樣:

trip_plus(City, Who) :- 
    trip(Who, Where), 
    (
    city(Where, _), City = Where; 
    city(City, Where); 
    country(Country, Where), city(City, Country) 
). 

不過,我個人覺得不太明顯。

+0

感謝您的回覆隊友。 我檢查了你在SWI-Prolog Portable中的兩個建議,它工作,雖然其他城市(多倫多)在查詢搜索中出現了兩次。 在gprolog(對於windows)中,我沒有像SWI那樣的「USA」頭像,只有在美國上市的城市,不包括多倫多。 鏈接:http://pastebin.com/vhF8NGbJ 謝謝。 –

+0

您可以附加正在加載測試的* .pl文件嗎? – mrhania

0

那麼,你並沒有將「John前往多倫多」列入事實清單,因此 它永遠不會出現。你可以修改你的代碼,如下所示:

%This is not needed => country(C). 

city(toronto). %here whatever city john visited 
trip(john). %the person's name 
trip_plus(X, T) :- trip(T,C), city(X,C) | trip(T) , city(X). 
+0

你好朋友。 是的,我添加了「約翰前往多倫多」的事實。 謝謝。 –