freakman305
Programmer
Hi everyone, im new to prolog and have just started learning it and i've just struck a problem which i just cant get my head around. heres the code and underneath is what i cant seem to figure out.
leader('adam smith').
leader('harry tills').
leader('robert johnson').
leader('niam popper').
leader('tom crow').
leader('tim burton').
leader('arnold ghets').
ingroup('adam smith',star).
ingroup('harry tills',star).
ingroup('robert johnson',elephant).
ingroup('niam popper',giraffe).
ingroup('tom popper',giraffe).
ingroup('tim burton',dog).
ingroup('arnold ghets',cat).
start('adam smith',1721).
start('harry tills',1743).
start('robert johnson',1812).
start('niam popper',1846).
start('tom crow',1916).
start('tim burton',1922).
start('arnold ghets',1945).
end('adam smith',1742).
end('harry tills',1754).
end('robert johnson',1827).
end('niam popper',1851).
end('tom crow',1922).
end('tim burton',1923).
end('arnold ghets',1951).
leaderduring(X,Y):-
start(Y,Z),
end(Y,W),
X >= Z,
X =< W.
leadersucceeded(X,Y):-
end(X,Z),
began(Y,W),
Z =< W.
listofleaders([]).
listofleaders([X | Rest]):-
leaderduring(X,Y),
print(Y),nl,
listofleaders(Rest).
groups([], In, In).
groups([Year | Years], In, Out):-
leaderduring(Year,Z),
ingroup(Z,X),
groups(Years,[Year | In],Out).
groups([ _Year | Years],In, Out):-
groups(Years, In, Out).
I need to make a rule called groups with three arguments, the first a list (of years), the second a group name. The rule should be queried with the first argument a list of years and the second a group name, and will result in the third argument instantiated to a list consisting of occurrences of ‘in’ or ‘out’ indicating whether or not the leader at the start of the corresponding year was of the given group. For example, query group([1947, 1870], cat, X). would result in X = [yes,
no]
Please help, i just cant seem to get it to work.
leader('adam smith').
leader('harry tills').
leader('robert johnson').
leader('niam popper').
leader('tom crow').
leader('tim burton').
leader('arnold ghets').
ingroup('adam smith',star).
ingroup('harry tills',star).
ingroup('robert johnson',elephant).
ingroup('niam popper',giraffe).
ingroup('tom popper',giraffe).
ingroup('tim burton',dog).
ingroup('arnold ghets',cat).
start('adam smith',1721).
start('harry tills',1743).
start('robert johnson',1812).
start('niam popper',1846).
start('tom crow',1916).
start('tim burton',1922).
start('arnold ghets',1945).
end('adam smith',1742).
end('harry tills',1754).
end('robert johnson',1827).
end('niam popper',1851).
end('tom crow',1922).
end('tim burton',1923).
end('arnold ghets',1951).
leaderduring(X,Y):-
start(Y,Z),
end(Y,W),
X >= Z,
X =< W.
leadersucceeded(X,Y):-
end(X,Z),
began(Y,W),
Z =< W.
listofleaders([]).
listofleaders([X | Rest]):-
leaderduring(X,Y),
print(Y),nl,
listofleaders(Rest).
groups([], In, In).
groups([Year | Years], In, Out):-
leaderduring(Year,Z),
ingroup(Z,X),
groups(Years,[Year | In],Out).
groups([ _Year | Years],In, Out):-
groups(Years, In, Out).
I need to make a rule called groups with three arguments, the first a list (of years), the second a group name. The rule should be queried with the first argument a list of years and the second a group name, and will result in the third argument instantiated to a list consisting of occurrences of ‘in’ or ‘out’ indicating whether or not the leader at the start of the corresponding year was of the given group. For example, query group([1947, 1870], cat, X). would result in X = [yes,
no]
Please help, i just cant seem to get it to work.