[KAPLAN]
|
Cevap : Prolog Örnekleri | Prolog Examples
Program 7 5 - a DCG for a Noun Phrase that will not terminate
np(np(NP1, NP2)) -->
np(NP1),
noun(NP2)
np(np(Det)) -->
det(Det)
det(det(the)) --> [the]
noun(noun(car)) --> [car]
Program 7 6 - a DCG for a Noun Phrase that will terminate because of the use of modified functors
np(np(NP1, NP2)) -->
np1(NP1),
noun(NP2)
np1(np(Det)) -->
det(Det)
det(det(the)) --> [the]
noun(noun(car)) --> [car]
Program 7 7 - a DCG for a Noun Phrase that will terminate because of the use of an history list
np(np(NP1, NP2), History0, History, S0, S) :-
\+ memb(entry(np, S0), History0),
np(NP1, [entry(np, S0)|History0], History1, S0, S1),
noun(NP2, [entry(noun, S1)|History1], History, S1, S)
np(np(Det), History0, History) -->
det(Det, History0, History)
det(det(the), History, History) --> [the]
noun(noun(car), History, History) --> [car]
% 1 - terminating
memb(Elem, [Elem|_])
% 2 - recursive
memb(Elem, [Head|Tail]) :-
\+ (Elem = Head),
memb(Elem, Tail)
Program 7 8 - a DCG for a Noun Phrase that will terminate because of the use of a limited history list
np(np(NP1, NP2), History0, History, S0, S) :-
\+ memb(entry(np, S0), History0),
np(NP1, [entry(np, S0)|History0], History1, S0, S1),
noun(NP2, [entry(noun, S1)|History1], History, S1, S)
np(np(Det), History0, History) -->
det(Det, History0, History)
det(det(the), _History, []) --> [the]
noun(noun(car), _History, []) --> [car]
Program 7 9 - a DCG version of sum_sublist/3
sum_sublist(Elem, List, Sum) :-
find_sublist(Elem, List, Sublist),
sum(Elem, 0, Sum, Sublist, _)
% 1 - terminating
find_sublist(Elem) -->
[Elem]
% 2 - recursive
find_sublist(Elem) -->
[Head],
{ \+ (Head = Elem)},
find_sublist(Elem)
% 1 - terminating
sum(0, Sum, Sum) --> []
% 2 - recursive
sum(Count, Sum, Sum2) -->
[Head],
{ Sum1 is Sum + Head,
Count1 is Count - 1},
sum(Count1, Sum1, Sum2)
|