Add CT331 Programming Paradigms

This commit is contained in:
2023-12-07 01:33:53 +00:00
parent 38a012c323
commit 262614ce83
207 changed files with 4516 additions and 0 deletions

View File

@ -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).