2011-10-29 68 views
1

以下事實表示這種格式序言中比較和數據過濾

%% movie(title , star, genre, gross) 
movie(battle_los_angeles, aaron_eckhart, action, 13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy, 5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi, 30400000). 
movie(mars_needs_moms, seth_green, animation, 1700000). 
movie(beastly, alex_pettyfer, romance, 13600000). 
movie(hall_Pass, owen_wilson, comdey, 31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 
  • 定義的規則,只有返回電影的所有冠軍,在票房前10部電影。
  • 定義一個規則,只返回所有的電影明星。
  • 定義返回具有相同流派的電影標題的規則。
  • 定義一個規則,根據總共 兩個電影名稱進行比較。

我的解決方案至今是:

domains 
s=symbol 
predicates 
nondeterm movie(s,s,s,integer) 
nondeterm title(s) 
nondeterm star(s) 
clauses 
%% movie(title , star, genre, gross) 
movie(battle_los_angeles, aaron_eckhart, action,13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy,5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi,30400000). 
movie(mars_needs_moms, seth_green, animation,1700000). 
movie(beastly, alex_pettyfer, romance,13600000). 
movie(hall_Pass, owen_wilson, comdey,31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 
title(X):-movie(X,_,_,_). 
star(X):-movie(_,X,_,_). 

goal 
star(X). 

我試圖解決的最後兩分,但我不知道怎麼...

  • 定義返回標題的規則具有相同流派的電影。
  • 定義一個規則,根據總共 兩個電影名稱進行比較。

回答

3

定義的規則,只有返回電影

標題(X)的所有標題: - 電影(X,,_)。

實際上,該謂詞返回一個電影標題,但作爲一個生成器做了很多次。返回所有這些元素或生成它們是一個很大的區別。

定義一個規則,根據毛重比較兩個電影片名。

你是什麼意思「規則比較」?它應初始化訂單類型,如(<)或(>)或(==) - compare_gross?或者它應該初始化元素的最大值max_gross

最後,我的解決辦法是:

%% movie(title , star, genre, gross) 
movie(battle_los_angeles, aaron_eckhart, action, 13500000). 
movie(rango, johnny_depp, animation, 51100000). 
movie(red_riding_hood, amanda_seyfried, fantasy, 5300000). 
movie(the_adjustment_bureau, matt_damon, sci_fi, 30400000). 
movie(mars_needs_moms, seth_green, animation, 1700000). 
movie(beastly, alex_pettyfer, romance, 13600000). 
movie(hall_Pass, owen_wilson, comedy, 31300000). 
movie(just_go_with_it, adam_sandler, comedy, 91200000). 
movie(unknown, liam_neeson, thriller, 56000000). 
movie(the_king_speech, colin_Firth, history, 126400000). 

% Define a rule that only returns all the titles of movies. 
titles(Titles) :- 
    findall(X, movie(X, _, _, _), Titles). 

% Define a rule that only returns all the stars of movies. 
stars(Stars) :- 
    findall(X, movie(_, X, _, _), Stars). 

get_by_genre(Genre, Titles) :- 
    findall(X, movie(X, _, Genre, _), Titles). 

% Define a rule that compares between two movie titles based on gross 
compare_gross(Title1, Title2, Ans) :- 
    movie(Title1, _, _, Gross1), 
    movie(Title2, _, _, Gross2), 
    compare(Ans, Gross1, Gross2). 

max_gross(Title1, Title2, TitleMax) :- 
    movie(Title1, _, _, Gross1), 
    movie(Title2, _, _, Gross2), 
    (
     (Gross1 > Gross2 
     , TitleMax = Title1 
     ) 
     ; 
     (Gross1 =< Gross2 
     , TitleMax = Title2 
     ) 
    ), 
    !. 

而你有這個

?- titles(X). 
X = [battle_los_angeles, rango, red_riding_hood, the_adjustment_bureau, mars_needs_moms, beastly, hall_Pass, just_go_with_it, unknown|...]. 

?- stars(X). 
X = [aaron_eckhart, johnny_depp, amanda_seyfried, matt_damon, seth_green, alex_pettyfer, owen_wilson, adam_sandler, liam_neeson|...]. 

?- get_by_genre(animation, X). 
X = [rango, mars_needs_moms]. 

?- compare_gross(rango, hall_Pass, Ans). 
Ans = (>). 

?- max_gross(rango, hall_Pass, Ans). 
Ans = rango. 
+0

謝謝U,做ü有序言的良好來源,我使用Visual序言 – xsari3x

+0

你做他的所有功課!但看起來不錯 – DaveEdelstein