Pages

Monday, April 9, 2012

Ch4

% ex4.1
% (a)
family((_,SurNamre,_,_),_,[]).
% (b)
child(person(Name,Surname,_,works(_,_))).
% (c)
family(person(_,_,_,unemployed),person(_,_,_,works(_,_)),_).
% (d)
family(Husband,Wife,Children),
dateofbirth(Husband,date(_,_,Year1)),
dateofbirth(Wife,date(_,_,Year2)),
(Year1-Year2>=15
;
Year2-Year1>=15
)
,member(Child,Children).



% ex4.2 different from answer //????????
twins(Child1,Child2):-
family(_,_,Children),
Child1(Name1,_,_,_),
Child2(Name2,_,_,_),
different(Name1,Name2),
dateofbirth(Child1,Date),
dateofbirth(Child2,Date).


% ex4.3
nth_member(1,[X|Tail],X).
nth_member(N,[Y|Tail],X):-
N1 is N-1,
nth_member(N1,Tail,X).



# ex4.4
The input string shrinls on each non-silent cycle, and it cannot shrink indefinitely.



% ex4.5 ??? should silent is before trans?
accepts(State,_,_):-
final(State).

accepts(State,[X|Rest],MaxMoves):-
MaxMoves>0,
trans(State,X,State2),
NewMax is MaxMoves-1,
accepts(State2,Rest,NewMax).

accepts(State,String,MaxMoves):-
MaxMoves>0,
silent(State,State2),
NewMax is MaxMoves-1,
accepts(State2,String,NewMax).


% ex4.7
% (a)
jump(X/Y,X2/Y2):-
(dxy(Dx,Dy)
;
dxy(Dy,Dx))
,
X2 is X+Dx,
Y2 is Y+Dy,
inboard(X2),
inboard(Y2).

dxy(2,1).
dxy(2,-1).
dxy(-2,1).
dxy(-2,-1).

inboard(P):-
P>=1,
P<=8.

% (b)
knightparh([square]).
knightpath([S1,S2|Rest]):-
jump(S1,S2),
knightpath([S2|Rest]).

% (c)
?- knightpath([2/1,R,5/4,S,X/8]).


.

No comments:

Post a Comment