天天看點

Prolog基本程式

編輯下面代碼;

male(di).
male(jianbo).
female(xin).
female(yuan).
female(yuqing).
father(jianbo,di).
father(di,yuqing).
mother(xin,di).
mother(yuan,yuqing).
grandfather(X,Y):-father(X,Z),father(Z,Y).
grandmother(X,Y):-mother(X,Z),father(Z,Y).
daughter(X,Y):-father(X,Y),female(Y).
           

存為father.pl;存到swi-prolog的bin目錄下;據說此程式描述了 誰是誰的爸爸 的問題;我還不太了解它的文法;prolog程式的字尾名為pl;

進入swi-prolog;使用consult來編譯儲存的pl檔案;出現下圖提示,編譯成功;

Prolog基本程式

文法還不熟悉;prolog變量是大寫字母開頭;輸入下圖語句,然後;prolog告訴你,”yuqing”的祖父是”jianbo”; 

Prolog基本程式

    有空繼續學習它的文法和基本語句;如果能用這語言編一個故障診斷程式也是很棒的;

繼續閱讀