2014-07-11 40 views
0

我開始學習序言,這裏是我堅持, 我有一個演繹分貝以下,使用的Prolog的演繹數據庫

employee(smith, accounting, 30000). 
employee(jones, accounting, 50000). 
employee(mary, accounting, 40000). 
employee(helen, payroll, 20000). 
employee(mike, payroll, 10000). 
department(accounting, jones, 3). 
department(payroll, helen, 2). 

我需要一個Prolog的謂詞,manager_higherSalary_biggerDept,找到的名字員工薪水超過4萬,管理部門僱員多於特定部門。

例如,要查找尺寸比工資更高的部門經理,使超過40000一年,查詢:

?- manager_higherSalary_biggerDept(M, 40000, payroll). 

應該給:M =瓊斯; 沒有

+2

你有什麼企圖? –

+0

實際上,你的分貝它不是 - 還是一個演繹分貝...... – CapelliC

回答

0

這是很簡單的(未測試):

manager_higherSalary_biggerDept(Name, SalaryThreshold, DeptName) :- 
    employee(Name, _, Salary), 
    Salary > SalaryThreshold, 
    department(DeptName, _, DeptThreshold), 
    department(_, Name, Dept), 
    Dept > DeptThreshold. 
0

Prolog是一個說明性語言。這意味着你要描述問題的解決方案,並讓它的推理引擎完成這項工作。所以...

manager_of_bigger_department_with_higher_salary(M , S , D) :- % 
    department(D,_,DS) ,           % get the size of the targer department 
    manages_department_larger_than(M,DS) ,      % find the manager of a larger department 
    has_higher_salary_than(M,S)         % who has a salary higher than the specified threshold 
    .                % 

manages_department_larger_than(M , D , T) :- 
    department(D,M,S) , 
    S > T 
    . 

has_salary_higher_than(E , T) :- 
    employee(E,_,S) , 
    S > T 
    .