Question 1. Five golfers, named Jules, Gilles, Jean, Joe and Ghislain often play a round of golf together (use small caps for the players' names in your program). Their occupations are, not necessarily in this order, mason, plasterer, carpenter, tinsmith and roofer. They have played a round of golf but at the end their score cards don't match. Their results were even numbers between 64 and 84.
• Ghislain played par with 72, beating the roofer by 2 strokes.
• Gilles had a bad day, and has ended with 10 strokes above par, or 12 strokes more than the mason.
• The tinsmith has beaten Joe by 4 strokes and has won the round.
• Jules has beaten the carpenter by 8 strokes, but did not win overall.
Find the occupation and the score of each player.
a) Design a predicate to enumerate all possible combinations for players where N is the name of the player, O is the occupation and S is the score.
?- player(N,O,S).
N = jules,
O = carpenter,
S = 64;
N = jules,
O = carpenter,
S = 66
and so on.
b) Express the above bullet list as constraints, e.g., as below. You will have to fill-in the dots with the corresponding Prolog logic.
constraintRoofer(player(N,O,S)) :- ...
constraintMason(...
constraintTinsmith(...
constraintCarpenter(...
constraintWin(...
c) Finally, you will need to apply all the constraints, using the goal:
scores(player(jules,M1,P1), player(jean,M2,P2),
player(gilles,M3,P3), player(joe,M4,P4), player(ghislain,M5,P5)).
Question 2. We saw in class how to represent a binary search tree in Prolog. Write a predicate lca (K1, K2, T) which allows one to find the smallest common ancestor of K1 and K2 in the tree T. The predicate lca shall print the subtree T such that the root is the smallest common ancestor of K1 and K2.
For example, given the tree
treeA (X):-X = t (73, t (31, t(5,nil,nil), nil), t (101, t (83, nil, t(97,nil,nil)), t(2016,nil,nil))).
We can issue the query
- treeA(X), lca(83,2016,X).
t(101,t(83,nil,t(97,nil,nil)),t(2016,nil,nil))
X = t(73, t(31, t(5, nil, nil), nil), t(101, t(83, nil, t(97, nil, nil)), t(2016, nil, nil))).