prolog黄道带标志程序错误

时间:2015-04-14 06:24:00

标签: prolog

嗨我在prolog中有以下代码并收到错误。     域         date = d(day,mon,year)         dm = da(day,mon)         day,mon,year = integer         sign = symbol

predicates
    zodiac_month(sign,dm,dm)
    find_sign(Date)

clauses
    zodiac_month(aquarius,da(20,1),da(18,2)).
    zodiac_month(pisces,da(19,2),da(20,3)).
    zodiac_month(aries,da(21,3),da(19,4)).
    zodiac_month(taurus,da(20,4),da(20,5)).
    zodiac_month(gemini,da(21,5),da(21,6)).
    zodiac_month(cancer,da(22,6),da(22,7)).
    zodiac_month(leo,da(23,7),da(22,8)).    
    zodiac_month(virgo,da(23,8),da(22,9)).    
    zodiac_month(libra,da(23,9),da(23,10)).
    zodiac_month(scorpio,da(24,10),da(21,11)).
    zodiac_month(sagittarius,da(22,11),da(21,12)).
    zodiac_month(capricorn,da(22,12),da(19,1)).

    find_sign(d(Day,Mon,Year)) :-
        zodiac_month(Sign,da(_,_),da(Day2,Mon2)),
        Mon2 = Mon,
        Day <= Day2,
        write("Zodiac sign is " , Sign),nl.

    find_sign(d(Day,Mon,Year)) :-
        zodiac_month(Sign,da(Day1,Mon1),da(_,_)),
        Mon1 = Mon,
        Day >= Day1,
        write("Zodiac Sign is " ,Sign),nl.

    find_sign(d(Day,Mon,Year)) :-
        write("Incorrect Date specified").

当我在prolog / flex中运行此代码时,它会出现以下错误:

ERROR: d:/zodiac.pl:2:3: Syntax error: Operator expected
ERROR: d:/zodiac.pl:28:11: Syntax error: Operator expected
Warning: d:/zodiac.pl:31:
        Singleton variables: [Year]
Warning: d:/zodiac.pl:37:
        Singleton variables: [Day,Mon,Year]

PLZ帮助我,因为它是我的学期项目。提前致谢

1 个答案:

答案 0 :(得分:1)

我几乎听说过prolog / flex。 OTOH,从错误/警告看来你正在运行SWI-Prolog。所以,这是修改后的代码:

zodiac_month(aquarius,da(20,1),da(18,2)).
zodiac_month(pisces,da(19,2),da(20,3)).
zodiac_month(aries,da(21,3),da(19,4)).
zodiac_month(taurus,da(20,4),da(20,5)).
zodiac_month(gemini,da(21,5),da(21,6)).
zodiac_month(cancer,da(22,6),da(22,7)).
zodiac_month(leo,da(23,7),da(22,8)).    
zodiac_month(virgo,da(23,8),da(22,9)).    
zodiac_month(libra,da(23,9),da(23,10)).
zodiac_month(scorpio,da(24,10),da(21,11)).
zodiac_month(sagittarius,da(22,11),da(21,12)).
zodiac_month(capricorn,da(22,12),da(19,1)).

find_sign(d(Day,Mon,_Year)) :-
    zodiac_month(Sign,da(_,_),da(Day2,Mon2)),
    Mon2 = Mon,
    Day =< Day2,
    format('Zodiac sign is ~w~n', [Sign]).

find_sign(d(Day,Mon,_Year)) :-
    zodiac_month(Sign,da(Day1,Mon1),da(_,_)),
    Mon1 = Mon,
    Day >= Day1,
    format('Zodiac Sign is ~w~n', [Sign]).

find_sign(Date) :-
    format('~w: Incorrect Date specified', [Date]).

请注意,保持代码可重用性更好,将表示与逻辑分开:

find_sign(d(Day,Mon,_Year),Sign) :-
    zodiac_month(Sign,da(_,_),da(Day2,Mon2)),
    Mon2 = Mon,
    Day =< Day2.
find_sign(d(Day,Mon,_Year),Sign) :-
    zodiac_month(Sign,da(Day1,Mon1),da(_,_)),
    Mon1 = Mon,
    Day >= Day1.

find_sign(Date) :-
    zodiac_month(Date,Sign),
    format('Zodiac Sign is ~w~n', [Sign]).
find_sign(Date) :-
    format('~w: Incorrect Date specified', [Date]).

@repeat在评论中突出显示,匹配可以表示为

find_sign(d(Day,Mon,_Year),Sign) :-
    zodiac_month(Sign,da(Day1,Mon1),da(Day2,Mon2)),
    ( Mon1 = Mon, Day >= Day1 ; Mon2 = Mon, Day =< Day2 ).

并且演示文稿应该使用&#39; if-then-else&#39;例如,构造

find_sign(Date) :-
    (   zodiac_month(Date,Sign)
    ->  format('Zodiac Sign is ~w~n', [Sign])
    ;   format('~w: Incorrect Date specified', [Date])
    ).

编辑计算年龄:

% age(+Date, +Birthday, -Age)
%
age(d(D1,M1,Y1), d(D2,M2,Y2), Age) :-
    A is Y1 - Y2,
    ( ( M1 < M2 ; M1 == M2, D1 < D2 ) -> Age is A - 1 ; Age = A).