Reputation: 1
I'm working on the following problem in Prolog:
Five patients, all having blood tests, are waiting in the doctor's surgery and are sitting on a bench from left to right, where the leftmost position is the first. Determine the position of each patient, along with her or his blood group, age, height, and weight. Their ages are 5, 9, 30, 46 and 60. Their heights are 40, 48, 60, 65 and 74. Their weights are 40, 75, 96, 125 and 165.
Define the predicate patients(Table) where Table is a table, where the rows from 1 to 5 describe the people sitting on the bench in left to right order. The columns are in the sequence person’s name, blood group, age, height and weight. An example value for Table could be Table = [['Adam', 'A', 5, 40, 40], ['Ali', 'AB', 9, 48, 75]], ['Alice', 'AO', 30, 60, 96], ['Farah', 'B', 46, 65, 125], ['Leila', 'O', 60, 74, 165]].
I think I am very close to the solution, though my program isn't running properly. I'll paste my code below:
:- op(100,xfy,on).
age(5). age(9). age(30). age(46). age(60).
height(40). height(48). height(60). height(65). height(74).
weight(40). weight(75). weight(96). weight(125). weight(165).
patients(Table) :-
makebench(5, Table),
Table = [_, _, _, _, [_, _, Age5, 60, _]],
['Leila', _, AgeLeila, HeightLeila, WeightLeila] on Table,
['Alice', _, _, 74, 75] on Table,
['Ali', 'AB', _, _, WeightAli] on Table,
Table = [_, _, [_, 'AO', 9, _, 96], _, _],
Table = [['Adam', _, _, 65, 165], _, _, _, _],
rightof([_, 'O', A, _, _], [_, _, A1, _, _], Table),
['Farah', _, 60, HeightFarah, _] on Table,
[_, 'A', 5, H, _] on Table,
Table = [_, _, _, [_, 'B', _, _, 125], _],
{ Age5 = AgeLeila + 37,
WeightLeila = HeightLeila + 56,
WeightAli = WeightLeila - 56,
A = A1 + 25,
H = HeightFarah + 17,
age(Age5),
age(AgeLeila),
weight(WeightLeila),
height(HeightLeila),
weight(WeightAli),
height(HeightFarah)}.
makebench(0, []).
makebench(N, [[_, _, _, _, _]|List]) :-
N > 0, N1 is N - 1, makebench(N1,List).
X on [X | _].
X on [_ | R] :- X on R.
sublist(S, L) :- add(S, _, L).
sublist(S, [_ | T]) :- sublist(S, T).
add([], L, L).
add([X | R], Y, [X | T]) :- add(R,Y,T).
rightof(H1, H2, L) :- sublist([H2, H1], L).
Any help is appreciated.
Upvotes: 0
Views: 164
Reputation: 1
I figured out the problem: I had to load the clpr library and remove the age(), height() and weight() terms from the constraints. Actually, I removed them entirely because they weren't needed.
Upvotes: 0