feat: 2104 assignment
This commit is contained in:
parent
fe0a63b2ba
commit
78c03662c5
@ -0,0 +1,38 @@
|
||||
-- Q1
|
||||
SELECT c.name
|
||||
FROM countries c
|
||||
WHERE c.population > 100000000
|
||||
AND c.continent = 'Africa';
|
||||
|
||||
SELECT c.continent, COUNT(*)
|
||||
FROM countries c
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM airports a
|
||||
WHERE c.iso2 = a.country_iso2
|
||||
)
|
||||
GROUP BY c.continent;
|
||||
|
||||
SELECT c.name, COUNT(*)
|
||||
FROM borders b, countries c
|
||||
WHERE c.iso2 = b.country1_iso2
|
||||
GROUP BY c.iso2
|
||||
ORDER BY COUNT(*) DESC
|
||||
LIMIT 10;
|
||||
|
||||
SELECT b.country1_iso2, b.country2_iso2
|
||||
FROM borders b, countries c1, countries c2
|
||||
WHERE b.country1_iso2 = c1.iso2
|
||||
AND b.country2_iso2 = c2.iso2
|
||||
AND c1.continent = 'Asia' AND c2.continent = 'Europe';
|
||||
|
||||
SELECT c.name
|
||||
FROM countries c
|
||||
WHERE NOT EXISTS(
|
||||
SELECT *
|
||||
FROM routes r, airports a, countries c1
|
||||
WHERE r.airline_code = 'SQ'
|
||||
AND c1.continent = 'Asia'
|
||||
AND r.to_code = a.code
|
||||
AND c1.iso2 = a.country_iso2
|
||||
) AND c.continent = 'Asia';
|
@ -0,0 +1,60 @@
|
||||
SELECT COUNT(*)
|
||||
FROM subzones
|
||||
WHERE population > 15000;
|
||||
|
||||
SELECT max_floor, COUNT(max_floor)
|
||||
FROM hdb_blocks
|
||||
GROUP BY max_floor
|
||||
ORDER BY COUNT(max_floor) desc
|
||||
LIMIT 1;
|
||||
|
||||
|
||||
SELECT *
|
||||
FROM areas a
|
||||
WHERE region = 'central'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM mrt_stations m,subzones s
|
||||
WHERE m.subzone = s.name
|
||||
AND s.area = a.name
|
||||
);
|
||||
|
||||
SELECT sz.area, COUNT(stop.code) AS num_stops
|
||||
FROM mrt_stops stop,
|
||||
mrt_stations s,
|
||||
subzones sz
|
||||
WHERE stop.station = s.name
|
||||
AND s.subzone = sz.name
|
||||
GROUP BY sz.area
|
||||
ORDER BY num_stops DESC
|
||||
LIMIT 3;
|
||||
|
||||
SELECT DISTINCT m1.station
|
||||
FROM mrt_connections c, mrt_stops m1, mrt_stops m2
|
||||
WHERE c.from_code = m1.code
|
||||
AND c.to_code = m2.code
|
||||
AND m1.line = 'ew'
|
||||
AND m2.line != 'ew';
|
||||
|
||||
SELECT sz.area, COUNT(DISTINCT so.line) AS num_lines
|
||||
FROM mrt_stops so,
|
||||
mrt_stations st,
|
||||
subzones sz
|
||||
WHERE so.station = st.name
|
||||
AND st.subzone = sz.name
|
||||
GROUP BY sz.area
|
||||
HAVING COUNT(DISTINCT so.line) >= 3
|
||||
ORDER BY num_lines DESC;
|
||||
|
||||
SELECT tab.area, COUNT(*) AS num_lines
|
||||
FROM (
|
||||
SELECT s.area, h.line
|
||||
FROM mrt_stops h,
|
||||
mrt_stations m,
|
||||
subzones s
|
||||
WHERE h.station = m.name
|
||||
AND m.subzone = s.name
|
||||
GROUP BY s.area, h.line
|
||||
) tab
|
||||
GROUP BY tab.area
|
||||
HAVING COUNT (*) >= 3 ORDER BY num_lines DESC
|
@ -0,0 +1,65 @@
|
||||
-- How many loans involve an owner and a borrower from the same department?
|
||||
SELECT COUNT(*)
|
||||
FROM loan l,
|
||||
student owner,
|
||||
student borrower
|
||||
WHERE l.owner = owner.email
|
||||
AND l.borrower = borrower.email
|
||||
AND owner.department = borrower.department;
|
||||
|
||||
-- For each faculty, print the number of loans that involve an owner and a borrower from this faculty?
|
||||
SELECT owner.department, COUNT(*)
|
||||
FROM loan l,
|
||||
student owner,
|
||||
student borrower
|
||||
WHERE l.owner = owner.email
|
||||
AND l.borrower = borrower.email
|
||||
AND owner.department = borrower.department
|
||||
GROUP BY owner.department;
|
||||
|
||||
-- What are the average and the standard deviation of the duration of a loan? Round the results to the nearest integer.
|
||||
SELECT ROUND(AVG(l.returned - l.borrowed+1),0), ROUND(stddev_pop(l.returned - l.borrowed+1),0)
|
||||
FROM loan l;
|
||||
|
||||
-- (a) Print the titles of the different books that have never been borrowed. Use a nested query.
|
||||
|
||||
SELECT b.title
|
||||
FROM book b
|
||||
WHERE b.isbn13 NOT IN (
|
||||
SELECT l.book
|
||||
FROM loan l
|
||||
);
|
||||
|
||||
-- (b) Print the name of the different students who own a copy of a book that they have never lent to anybody.
|
||||
SELECT s.name
|
||||
FROM student s
|
||||
WHERE s.email IN (
|
||||
SELECT c.owner
|
||||
FROM copy c
|
||||
WHERE (c.owner, c.book,c.copy)NOT IN (
|
||||
SELECT l.owner,l.book,l.copy
|
||||
FROM loan l
|
||||
) );
|
||||
|
||||
-- For each department, print the names of the students who lent the most.
|
||||
SELECT s.name,s.department, COUNT(*)
|
||||
FROM student s,loan l
|
||||
WHERE l.owner = s.email
|
||||
GROUP BY s.name, s.department
|
||||
HAVING COUNT(*) >= ALL
|
||||
(SELECT COUNT(*) FROM student s1, loan l1 WHERE l1.owner = s1.email AND s.department = s1.department GROUP BY s1.email);
|
||||
|
||||
-- Print email and name of different students who borrowed all books authored by adam smith
|
||||
SELECT s.email, s.name
|
||||
FROM student s
|
||||
WHERE NOT EXISTS(
|
||||
SELECT *
|
||||
FROM book b
|
||||
WHERE b.authors = 'Adam Smith'
|
||||
AND NOT EXISTS(
|
||||
SELECT *
|
||||
FROM loan l
|
||||
WHERE l.book = b.isbn13
|
||||
AND l.borrower = s.email
|
||||
)
|
||||
)
|
@ -0,0 +1,43 @@
|
||||
-- How many loans involve
|
||||
SELECT b.title
|
||||
FROM book b
|
||||
WHERE b.isbn13 != ALL (
|
||||
SELECT l.book FROM loan l
|
||||
);
|
||||
-- 2b
|
||||
SELECT s.name
|
||||
FROM student s
|
||||
WHERE s.email = ANY (
|
||||
SELECT c.owner
|
||||
FROM copy c
|
||||
WHERE (c.owner, c.book, c.copy) NOT IN (
|
||||
SELECT l.owner, l.book, l.copy
|
||||
FROM loan l
|
||||
)
|
||||
);
|
||||
|
||||
-- 2c (When you see the most/the least, there's a standard way
|
||||
-- of solving this
|
||||
|
||||
SELECT s.department, s.name, COUNT(*)
|
||||
FROM student s, loan l
|
||||
WHERE l.owner = s.email
|
||||
GROUP BY s.department, s.name, s.email
|
||||
HAVING COUNT(*) >= ALL (
|
||||
SELECT COUNT(*)
|
||||
FROM student s1, loan l1
|
||||
WHERE l1.owner = s1.email
|
||||
AND s.department = s1.department
|
||||
GROUP BY s1.email
|
||||
)
|
||||
ORDER BY COUNT(*) DESC;
|
||||
|
||||
-- 2d
|
||||
|
||||
SELECT s.name, s.email
|
||||
FROM student s
|
||||
WHERE s.email = ALL (
|
||||
SELECT c
|
||||
FROM book b
|
||||
WHERE b.authors LIKE '%Adam Smith%'
|
||||
)
|
0
labs/cs2102/project/part3.sql
Normal file
0
labs/cs2102/project/part3.sql
Normal file
311
labs/cs2104/prolog/assignment.pl
Normal file
311
labs/cs2104/prolog/assignment.pl
Normal file
@ -0,0 +1,311 @@
|
||||
%% In Prolog, atoms, numbers, and pairs (lists)
|
||||
%% are data structures. Furthermore, We can construct a new
|
||||
%% data structure using "function" symbols that are not
|
||||
%% further interpreted. So
|
||||
%% f(10, 20, 30)
|
||||
%% is a data structure with the label 'f' and three components,
|
||||
%% the numbers 10, 20, and 30.
|
||||
|
||||
%% A binary number tree is either null or
|
||||
%% a node(T1, V, T2) where T1 is
|
||||
%% a binary number tree, V is an number, and T2 is a
|
||||
%% binary number tree.
|
||||
|
||||
%% Note that 'node' is the label, and T1, V, and T2 are
|
||||
%% the components of a node data structure.
|
||||
|
||||
%% Examples:
|
||||
|
||||
tree1(
|
||||
node(node(node(null,
|
||||
1,
|
||||
null),
|
||||
2,
|
||||
node(null,
|
||||
3,
|
||||
null)),
|
||||
4,
|
||||
node(node(null,
|
||||
5,
|
||||
null),
|
||||
6,
|
||||
node(null,
|
||||
7,
|
||||
null)))
|
||||
).
|
||||
|
||||
tree2(null).
|
||||
tree3(node(null,
|
||||
4,
|
||||
null)).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 1, 5 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Consider the following predicate leftmost_element:
|
||||
|
||||
leftmost_element(node(null, V, _), V).
|
||||
leftmost_element(node(T1, _, _), W) :-
|
||||
leftmost_element(T1, W).
|
||||
|
||||
test_leftmost_element :-
|
||||
tree1(T1), leftmost_element(T1, 1),
|
||||
tree2(T2), not(leftmost_element(T2,_)),
|
||||
tree3(T3), leftmost_element(T3, 4).
|
||||
|
||||
%% Explain the behavior of the query
|
||||
%% leftmost_element(X, 4) in three or four sentences:
|
||||
%% T = node(null, 4, _) ;
|
||||
%% T = node(node(null, 4, _), _, _) ;
|
||||
%% T = node(node(node(null, 4, _), _, _), _, _) ;
|
||||
%% T = node(node(node(node(null, 4, _), _, _), _, _), _, _)
|
||||
|
||||
%% Write your answer in comments here:
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
%%
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 2, 10 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% The depth of a tree is defined to be the longest path
|
||||
%% from the root of the tree to any value, and 0 if there
|
||||
%% are no values in the tree.
|
||||
|
||||
%% Write a predicate depth(T, X) that holds if X is the
|
||||
%% depth of T.
|
||||
|
||||
%%%%%%%%%
|
||||
%% your solution goes here
|
||||
|
||||
depth(null, 0).
|
||||
depth(node(Left, _, Right), Res) :-
|
||||
depth(Left, F1),
|
||||
depth(Right, F2),
|
||||
Res is max(F1, F2) + 1.
|
||||
%%%%%%%%%
|
||||
|
||||
test_depth :-
|
||||
tree1(T1), depth(T1, 3),
|
||||
tree2(T2), depth(T2, 0),
|
||||
tree3(T3), depth(T3, 1).
|
||||
|
||||
%% test your solution by
|
||||
%% writing
|
||||
%% ?- test_depth.
|
||||
%% in the SWI-Prolog REPL.
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 3, 10 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Write a predicate sum(T, S) that computes the sum S
|
||||
%% of all elements of a tree T.
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% your solution goes here
|
||||
sum(null, 0).
|
||||
sum(node(Left, X, Right), Res) :-
|
||||
sum(Left, LeftRes),
|
||||
sum(Right, RightRes),
|
||||
Res is X + LeftRes + RightRes.
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_sum :-
|
||||
tree1(T1), sum(T1, 28),
|
||||
tree2(T2), sum(T2, 0),
|
||||
tree3(T3), sum(T3, 4).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question X, 0 points, just practice
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Write a predicate bt_member(T, V) that holds
|
||||
%% if and only if V is a value in the tree T.
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% solution at the end of this file
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_bt_member :-
|
||||
tree1(T1), bt_member(T1, 5),
|
||||
tree2(T2), not(bt_member(T2, _)),
|
||||
tree3(T3), bt_member(T3, 4).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question Y, 0 points, just practice
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Note that the given trees in tree1, tree2, and tree3
|
||||
%% are binary search trees: For every node, the values in
|
||||
%% the left subtree of the node are all smaller than the
|
||||
%% value, and the values in the right subtree of the tree
|
||||
%% are all larger than the value.
|
||||
|
||||
%% Write a predicate bst_member(T, V) that exploits this fact
|
||||
%% and that holds if and only if V is a value in the
|
||||
%% binary search tree T. The number of recursive calls
|
||||
%% should be limited by the depth of the binary search tree.
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% solution at the end of this file
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_bst_member :-
|
||||
tree1(T1), bst_member(T1, 5),
|
||||
tree2(T2), not(bst_member(T2, _)),
|
||||
tree3(T3), bst_member(T3, 4).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question Z, 0 points, just practice
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Write a predicate make_bst(N, T) that makes a binary search
|
||||
%% tree that contains the integers 1,..,N.
|
||||
%% Hint: Remember Question 1
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% solution at the end of this file
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_make_bst :-
|
||||
make_bst(10, T1), bst_member(T1, 10),
|
||||
not(bst_member(T1, 11)),
|
||||
make_bst(100, T2), bst_member(T2, 100),
|
||||
not(bst_member(T2, 101)).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 4, 10 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%% Here is a length predicate for lists.
|
||||
len([], 0).
|
||||
len([_|T], N) :-
|
||||
len(T, N1), N is N1 + 1.
|
||||
|
||||
test_length :-
|
||||
len([1,2,3,4], 4),
|
||||
len([], 0),
|
||||
len([1,2,3,4,5,6,7,8,9,10], 10).
|
||||
|
||||
%% Write a predicate nth(I, L, V) that holds
|
||||
%% if and only if V is the nth element of list L,
|
||||
%% assuming we start counting at 0.
|
||||
%%
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% your solution goes here
|
||||
%% The 0th element of any list is V
|
||||
nth(0, [V|_], V).
|
||||
nth(Idx, [_|Tail], Elem) :-
|
||||
Idx > 0, %% Catch for negative indices
|
||||
Idx1 is Idx - 1,
|
||||
nth(Idx1, Tail, Elem).
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_nth :-
|
||||
nth(0, [1,2,3,4], 1),
|
||||
not(nth(0, [], 0)),
|
||||
nth(9, [1,2,3,4,5,6,7,8,9,10], 10).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 5, 10 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%% Most likely, your nth predicate is not going
|
||||
%% to work backwards. It doesn't allow you to
|
||||
%% identify the position of a given value in
|
||||
%% a list: nth(I, [10,20,30,40], 30) will not bind
|
||||
%pos(
|
||||
%% I to the value 2. Do you see why? (no submission)
|
||||
%%
|
||||
%% Write a predicate pos(X, L, I) that holds when
|
||||
%% I is a position at which L has the value X.
|
||||
%% Thus pos(30, [10,20,30,40], I) should bind
|
||||
%% I to the value 2.
|
||||
|
||||
%% Make sure that your predicate will hold for
|
||||
%% all positions of the given value X, not just
|
||||
%% the first one. Thus pos(30, [10, 30, 30, 40], I)
|
||||
%% should bind I to 1, and upon backtracking, it
|
||||
%% should bind I to the value 2.
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% your solution goes here
|
||||
|
||||
|
||||
pos(Target, List, Index) :- pos_helper(Target, List, 0, Index).
|
||||
pos_helper(Target, [Target|_], Acc, Acc).
|
||||
pos_helper(Target, [_|Tail], Acc, Index) :-
|
||||
Acc1 is Acc + 1,
|
||||
pos_helper(Target, Tail, Acc1, Index).
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_pos :-
|
||||
pos(10, [10,20,30,40], 0),
|
||||
not(pos(0, [], _)),
|
||||
not(pos(0, [1,2,3,4], _)),
|
||||
pos(20, [10,20,20,40], 2),
|
||||
pos(10, [1,2,3,4,5,6,7,8,9,10], 9).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Question 6, 10 points
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%% Write a predicate all_pos(X, L, I) that
|
||||
%% computes the list of all positions at which
|
||||
%% L has the value X. Thus
|
||||
%% all_pos(30, [10, 30, 30, 40], X) should bind
|
||||
%% X to the list [1, 2].
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
%% your solution goes here
|
||||
|
||||
all_pos(Target, List, Res) :- all_pos_helper(Target, List,0, Res).
|
||||
all_pos_helper(_, [],_, []).
|
||||
all_pos_helper(Target, [Target|Tail], Acc, [Acc|Rest]) :-
|
||||
Acc1 is Acc + 1,
|
||||
all_pos_helper(Target, Tail, Acc1, Rest).
|
||||
all_pos_helper(Target, [_|Tail], Acc, Rest) :-
|
||||
Acc1 is Acc + 1,
|
||||
all_pos_helper(Target, Tail, Acc1, Rest).
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_all_pos :-
|
||||
all_pos(0, [1,2,3,4], []),
|
||||
all_pos(0, [], []),
|
||||
all_pos(10, [1,2,3,4,5,6,7,8,9,10], [9]),
|
||||
all_pos(4, [1,2,3,4,4,4,4,4,9,10], [3,4,5,6,7]).
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
%% Solution Question X
|
||||
bt_member(node(_, V,_), V).
|
||||
bt_member(node(T1,_,_), V) :- bt_member(T1, V).
|
||||
bt_member(node(_,_,T2), V) :- bt_member(T2, V).
|
||||
|
||||
%% Solution Question Y
|
||||
bst_member(node(_, V, _), V).
|
||||
bst_member(node(T1,V,_), W) :- W < V, bst_member(T1, W).
|
||||
bst_member(node(_,V,T2), W) :- W > V, bst_member(T2, W).
|
||||
|
||||
%% Solution Question Z
|
||||
make_bst(0, null).
|
||||
make_bst(N, node(T1, N, null)) :-
|
||||
N > 0, N1 is N - 1, make_bst(N1, T1).
|
||||
|
||||
|
37
labs/cs2104/prolog/family.pl
Normal file
37
labs/cs2104/prolog/family.pl
Normal file
@ -0,0 +1,37 @@
|
||||
parent(a, b).
|
||||
parent(a, c).
|
||||
parent(b, d).
|
||||
parent(b, e).
|
||||
parent(c, f).
|
||||
parent(c, g).
|
||||
parent(f, h).
|
||||
parent(f, i).
|
||||
|
||||
ancestor(X, Y) :- parent(X, Y).
|
||||
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
|
||||
|
||||
factorial(1, 1).
|
||||
factorial(N, F) :- N > 1, N1 is N - 1, factorial(N1, F1), F is F1 * N.
|
||||
|
||||
faciter(1, Res, Res).
|
||||
faciter(N, Acc, Res) :- N > 1, N1 is N -1, Acc1 is N * Acc, faciter(N1, Acc1, Res).
|
||||
|
||||
fib(0, 0).
|
||||
fib(1, 1).
|
||||
fib(N, F) :- N > 1, N1 is N-1, N2 is N-2, fib(N1, F1), fib(N2, F2), F is F1 + F2.
|
||||
|
||||
fibiter(X, X, F1, F2, F1).
|
||||
fibiter(X, N, F1, F2, Result) :- X < N, X1 is X + 1, Aux is F1 + F2, fibiter(X1, N, F2, Aux, Result).
|
||||
|
||||
head([H|_], H).
|
||||
tail([_|T], T).
|
||||
|
||||
member(X, [X|_]).
|
||||
member(X, [_|T]) :- member(X, T).
|
||||
|
||||
append([], L, L).
|
||||
append([H|T], L, [H|R]) :- append(T, L, R).
|
||||
|
||||
reverse([], []).
|
||||
reverse([H|T], R) :-
|
||||
reverse(T, R1), append(R1, [H], R).
|
44
labs/cs2104/prolog/likes.pl
Normal file
44
labs/cs2104/prolog/likes.pl
Normal file
@ -0,0 +1,44 @@
|
||||
%% Demo coming from http://clwww.essex.ac.uk/course/LG519/2-facts/index_18.html
|
||||
%%
|
||||
%% Please load this file into SWI-Prolog
|
||||
%%
|
||||
%% Sam's likes and dislikes in food
|
||||
%%
|
||||
%% Considering the following will give some practice
|
||||
%% in thinking about backtracking.
|
||||
%%
|
||||
%% You can also run this demo online at
|
||||
%% http://swish.swi-prolog.org/?code=https://github.com/SWI-Prolog/swipl-devel/raw/master/demo/likes.pl&q=likes(sam,Food).
|
||||
|
||||
/** <examples>
|
||||
?- likes(sam,dahl).
|
||||
?- likes(sam,chop_suey).
|
||||
?- likes(sam,pizza).
|
||||
?- likes(sam,chips).
|
||||
?- likes(sam,curry).
|
||||
*/
|
||||
|
||||
likes(sam,Food) :-
|
||||
indian(Food),
|
||||
mild(Food).
|
||||
likes(sam,Food) :-
|
||||
chinese(Food).
|
||||
likes(sam,Food) :-
|
||||
italian(Food).
|
||||
likes(sam,chips).
|
||||
|
||||
indian(curry).
|
||||
indian(dahl).
|
||||
indian(tandoori).
|
||||
indian(kurma).
|
||||
|
||||
mild(dahl).
|
||||
mild(tandoori).
|
||||
mild(kurma).
|
||||
|
||||
chinese(chow_mein).
|
||||
chinese(chop_suey).
|
||||
chinese(sweet_and_sour).
|
||||
|
||||
italian(pizza).
|
||||
italian(spaghetti).
|
Loading…
Reference in New Issue
Block a user