Prolog programming exercises ------ ----------- --------- Set 1: (Simple) --- - 1. Write Prolog programs to calculate: i) Factorial n. ii) Ackermann's function a(m,n) which is defined as a(0,n)=n+1 a(m,0)=a(m-1,1) a(m,n)=a(m-1,a(m,n-1)) if m>0,n>0. { Warning: You are strongly advised to avoid using values of m>4 when testing this function } 2. Using the assert facility, produce a more efficient version of your Ackermann program. { Warning: The previous warning still applies } Set 2: (More complex) --- - 1. The file 'cricket' contains some information about England-Australia Test Matches: century(Name,Year,Score,Ground). australia(Ground). england(Ground). australian(Name). english(Name). Write Prolog programs to: (i) print out a list of all batsmen who scored a Test double century (200 runs or more ) while on tour in the 1920s. (ii) find all the batsmen who have scored Test centuries on three or more different grounds,listing the full details in each case. (iii) find which batsman/men who has/have scored the most number of centuries on any one ground. 2. Write a Prolog program which describes the following circuit, and use it to find the outputs D,E for all possible combinations of the inputs A, B and C. ---\ ----------------| \ | | o--------- | ------| / | ---\ | | ---/ --------| \ | ---\ | | o----------- D A ---*--| \ | --------| / | o-* | ---/ B ---*--| / | | | ---/ | | | | |\ | ----\ | *-----| o-----------+------\ \ | | |/ | ) o----------- E | | | ---/ / | | ---\ | | ----/ | ------| \ | | | | X o--------- | *---------------| / | | ---/ | | ---\ | -----| \ | | o---- | C ---*----| / | | | ---/ | | | | ---\ | | -------| \ | | | o-------- ---------------------| / ---/ Note. ---\ | \ | o represents a NAND gate (not and) | / ---/ The other devices are an inverter and a NOR gate (not or). Repeat the calculation in the case when the device marked X in the circuit is faulty and always produces an output 0 for all input combinations. 3. Using the file 'england', write programs to: (i) print a list of all counties which are adjacent to three or more counties. (ii) print a list of all the towns which are less than 65 miles from Bristol, listing them by county. (iii) print a list of counties through which you would travel in order to get to Kent from Cornwall. 4. The file 'tennis' contains information about some tennis players. champion(Name). male(Name). female(Name). child(Child,Parent). older(Name1,Name2). Name1 is older than Name2. born_before(Name1,Name2). Name1 was born before Name2. Write Prolog programs which attempt to answer the following questions: (i) Which female champion is older than Kelly? (ii) Which male champions are younger than Kelly? (iii) Is Borg older than Evert? [Hint: You may find it helpful to define a new predicate older_than(X,Y).] 5. At the annual pickling contest in the village of Much Acetic in the Marsh, there were four classes of produce: onions, gherkins, eggs and walnuts. Four competitors, Carol, Kate, Linda and Nicola, whose surnames, not necessarily in that order, are Roberts, Jenkins, Graham and Barker, each entered one of the classes and they each won a prize; one of the prizes was a first, one a second, one a third and one a fourth. Linda Roberts was not the lady whose gherkins were placed fourth. Mrs. Graham won first prize; this was not Carol. Kate won third. Mrs. Barker offered her pickled eggs. Nicola did not bring pickled onions. Find the full names of the four ladies, together with the class each entered and the prize which each won.