Add CT331 Programming Paradigms
This commit is contained in:
@ -0,0 +1,26 @@
|
||||
takes(tom, ct331).
|
||||
takes(mary, ct331).
|
||||
takes(joe, ct331).
|
||||
takes(tom, ct345).
|
||||
takes(mary, ct345).
|
||||
instructs(bob, ct331).
|
||||
instructs(ann, ct345).
|
||||
|
||||
% 1. rule that returns true if a given instructor teaches a given student
|
||||
teaches(Instructor, Student) :- instructs(Instructor, Course), takes(Student, Course).
|
||||
|
||||
% 2. query that uses the `teaches` rule to show all students instructed by bob
|
||||
?- teaches(bob, Student).
|
||||
?- findall(Student, teaches(bob, Student), Students).
|
||||
|
||||
% 3. query that uses the `teaches` rule to show all instructors that instruct mary
|
||||
?- teaches(Instructor, mary).
|
||||
?- findall(Instructor, teaches(Instructor, mary), Instructors).
|
||||
|
||||
% 5. rule that returns true if two students take the same course
|
||||
takesSameCourse(Student1, Student2) :- takes(Student1, Course), takes(Student2, Course).
|
||||
|
||||
contains1(Element, [Element | Tail]).
|
||||
|
||||
|
||||
contains2(Sublist, [Head | Sublist]).
|
@ -0,0 +1,5 @@
|
||||
% base case: any element is not in an empty list
|
||||
isNotElementInList(_, []).
|
||||
|
||||
% return true if Element is not the Head of the list and it's not found recursively searching the rest of the list
|
||||
isNotElementInList(Element, [Head | Tail]) :- Element \= Head, isNotElementInList(Element, Tail).
|
@ -0,0 +1,16 @@
|
||||
% predicate to merge two lists
|
||||
% base case: if the first list is empty, just return the second
|
||||
mergeTwoLists([], List, List).
|
||||
|
||||
% recursive predicate to merge two lists
|
||||
% split the first list into head and tail, and recurse with its tail and the second list until the first list is empty (base case)
|
||||
% then merge the original head of the first list with the resulting tail
|
||||
mergeTwoLists([Head | Tail], List2, [Head | ResultTail]) :- mergeTwoLists(Tail, List2, ResultTail).
|
||||
|
||||
% predicate to merge 3 lists
|
||||
% base case: merging an empty list and two others is the same as merging two lists
|
||||
mergeLists([], List2, List3, Merged) :- mergeTwoLists(List2, List3, Merged).
|
||||
|
||||
% split the first list into head and tail, and recurse with its tail and the other two lists until the first list is empty (base case)
|
||||
mergeLists([Head1 | Tail1], List2, List3, [Head1 | MergedTail]) :- mergeLists(Tail1, List2, List3, MergedTail).
|
||||
|
@ -0,0 +1,8 @@
|
||||
% call the helper predicate with the list to be reversed and an empty Accumulator to build up
|
||||
reverseList(List, Reversed) :- reverseListHelper(List, [], Reversed).
|
||||
|
||||
% base case fact: when the list to reverse is empty, the accumulator is the reversed list
|
||||
reverseListHelper([], Accumulator, Accumulator).
|
||||
|
||||
% recurse with the tail after prepending the head to the accumulator
|
||||
reverseListHelper([Head | Tail], Accumulator, Reversed) :- reverseListHelper(Tail, [Head | Accumulator], Reversed).
|
@ -0,0 +1,8 @@
|
||||
% base fact: if the list is empty, the list to be returned is just the element
|
||||
insertInOrder(Element, [], [Element]).
|
||||
|
||||
% if the element to be inserted is <= the head of the list, insert it at the head of the list
|
||||
insertInOrder(Element, [Head | Tail], [Element, Head | Tail]) :- Element =< Head.
|
||||
|
||||
% if the element to be inserted is greater than the head of the list, recurse with the tail of the list until
|
||||
insertInOrder(Element, [Head | Tail], [Head | NewTail]) :- Element > Head, insertInOrder(Element, Tail, NewTail).
|
Reference in New Issue
Block a user