% Jamie's coursework template % My Name here, My UserID, My Regno (why not) <--- so we know who you are % F28PL Coursework PR1 <--- sanity check % You may assume variables, procedures, and functions defined in earlier questions % in your answers to later questions, though you should add comments in code explaining % this if any clarification might help read your code. % The file extension of this template is .txt. It should be .pl, but this does not play nice with TurnItIn. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Question 1 <--- Yes, so we know what question you think you're answering % % The complex numbers are explained here (and elsewhere): % http://www.mathsisfun.com/algebra/complex-number-multiply.html % Represent a complex integer as a two-element list of integers, so [4,5] represents 4+5i. % Write Prolog predicates % cadd/3 % cmult/3 % representing complex integer addition and multiplication. Thus for instance, % cadd([X1,X2],[Y1,Y2],[Z1,Z2]) % succeeds if and only if Z1=X1+Y1 and Z2=X2+Y2. % Note that complex number multiplication is not just like complex number addition. % Check the link and read the definition. % % <--- always have the question under your nose /* Include testing in comments, so your marker can load this file as a database then cut-and-paste any testing into the command line. */ % END ANSWER TO Question 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Question 2 % % An integer sequence is a list of integers. Write a Prolog predicate % seqadd/3 % such that seqadd(X,Y,Z) succeeds when X and Y are lists of integers of the same length and % Z is their sequence sum. /* Include testing in comments, so your marker can load this file as a database then cut-and-paste any testing into the command line. */ % END ANSWER TO Question 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Question 4 % % 4a. Explain what backtracking has to do with Prolog. You might find this webpage helpful: % https://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/search.html % % 4b. Explain to what extent Prolog can be viewed as a logic programming language, and to what % extent it cannot be so viewed. Include example code fragments as appropriate. /* Write answer in comments here. */ % END ANSWER TO Question 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Question 5 % % Write a database for a predicate cycleoflife/1 such that the query % cycleoflife(X) % returns the instantiations % X = eat % X = sleep % X = code % X = eat % X = sleep % X = code % ... % in an endless cycle. % (This question has a beautiful and simple answer. If you find yourself writing lines and lines of % complex code, there’s probably something amiss.) /* Include testing in comments, so your marker can load this file as a database then cut-and-paste any testing into the command line. */ % END ANSWER TO Question 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%