Who is the mother of beatrice

Assignment Help Programming Languages
Reference no: EM131372372

Declarative Programming (Prolog) Assignment

Question 1: Recursion, Lists and Accumulating Parameters

(a) Write the following program and compile it:

% Program:         ROYAL

parent(queenmother,elisabeth).             parent(elisabeth,charles).

parent(elisabeth,andrew).                      parent(elisabeth,anne).

parent(elisabeth,edward).                      parent(diana,william).

parent(diana,harry).                              parent(sarah,beatrice).

parent(anne,peter).                               parent(anne,zara).

parent(george,elisabeth).                       parent(philip,charles).

parent(philip,andrew).                            parent(philip,edward).

parent(charles,william).                          parent(charles,harry).

parent(andrew,beatrice).                        parent(andrew,eugene).

parent(mark,peter).                               parent(mark,zara).

parent(william,georgejun).                      parent(kate,georgejun).

parent(kate,charlotte).

Define the following predicates on the persons in the program ROYAL.

(1) the_royal_females/1 (a list of all female members of the Royal Family)

(2) the_royal_males/1 (a list of all male members of the Royal Family)

(3) the_royal_family/1 (a list of all members of the Royal Family)

(4) mother/2

(5) has_child/1.

(6) ancestor/2 (use recursion).

(7) descendant/2 (use recursion or (6)).

Translate the following questions into Prolog queries and try them out:

(8) Who is the mother of Beatrice?

(9) Who has a child (one or more)?

(10) Who is a desencendant of the Queenmother?

(b) Use predicates of question (a) to define predicates sibling/2 and aunt/2 (w.r.t. the Royal Family). [Query: Who are the siblings of charles?]

(c) Write a predicate palindrome_list(L) which checks whether L is a palin-dromic list (i.e., reads the same forwards and backwards). Examples are [a,b,c,b,a] and [12,a,5,a,12]. The base cases are when the list is empty or a singleton. The general case is to check that the first element is the same as the last element and (recursively) the remaining part of the list is palindromic. To achieve this write a predicate last_element(L,X,R) which instantiates X to the last element of L and R to L with X removed. The easiest way to de?ne this is to use append.

(d) An example of a recursive predicate and a tail recursive version using an accumulating parameter.

i. The square of the Euclidean distance between two vectors xi and yi is i=1n(xi - yi)*(xi - yi). Write a recursive predicate euclidsqr(X,Y,ED) which returns the value in ED when X and Y are lists representing vectors of the same length.

ii. Now write a tail recursive predicate euclidsqr_acc(X,Y,A,ED) to compute the same function using the accumulating parameter A to store intermediate calculations. (Look at sum_a in example prolog code).

Question 2: Backtracking Solution of Futoshiki Puzzle

Here is a Futoshiki puzzle downloaded from the internet (popular in many news-papers).

The aim is to place digits 1-4 in the empty cells so that each row and column contains distinct digits and the constraints specified by the inequality signs are all satisfied. You are to write a generate and test backtracking program in Prolog to solve this puzzle.

(a) Define the predicate member_rem(E,L,R) which chooses an element E from list L leaving remainder R.

1543_Figure.png

(b) Using the above de?ne gen_list_n(N,D,L) which generates a list L of N distinct elements from the list D where the length of D is ≥ N.

gen_n(0,_,[]).

gen_n(N,D,[X|Xs]) :-

N>0, N1 is N-1,

....

gen_n(N1,D1,Xs).

Define gen4(L) to generate a list of 4 distinct digits from 1-4.

(c) To check that two list of numbers X, Y are different at each entry (i.e, Xi differs from Yi for all i) de?ne a predicate distinct_in_entries(X, Y). As elements of the lists are numbers you use =\= to check for inequality.

(d) Now you can generate a possible solution [R1,R2,R3,R4] where Ri are rows of 4 distinct numbers from 1-4 and all the columns consist of distinct numbers as follows: generate R1 (using gen4), then R2 and check R1 and R2 are distinct at all entries; generate R3 and check this is distinct in entries with R1 and R2 and so on. Call this predicate gen_poss_soln([R1,R2,R3,R4]). So e.g., it will produce [[1,2,3,4],[2,1,4,3],[3,4,1,2],[4,3,2,1]] as an output.

 (e) Finally define solve([R1,R2,R3,R4]) using generate and test by generating a possible solution, [R1,R2,R3,R4], and then testing each of the in-equalities. Notice that the only constraints in R1 are on R11 and R12 (R11 > R12) so you only need say R1 = [R11,R12,_,_]. You should deal with the other rows and constraints in a similar manner.

(f) The solver solve will ?nd the solution for a 4x4 Futoshiki problem in a reasonable time, but if you scaled it up in an obvious manner for 5x5 problems it would be too inefficient. So produce a more efficient version of solve which splits up the generate and test tasks. Call this solve_in_steps([R1,R2,R3,R4]) and this time, generate R1 first and test any constraints you can, just involving row 1 (in this case its only R11 > R12), then generate R2 and apply the constraints on any variables in R1 and R2 and so on. This approach should be able to cope with 5x5 problems (though there are many other improvements you can make to speed up the solver).

Reference no: EM131372372

Questions Cloud

What should be the companys decision : A random sample of 30 days gives a sample mean of 1,482 and the population standard deviation is 430. Construct a 95% confidence interval. What should be the company's decision?
Should the airline offer to fly the route based : Recently, three new airlines, MAXjet, L'Avion, and Eos, began operations selling business or first-class-only service.- Construct a 90% confidence interval.- Should the airline offer to fly this route based on your answer to part (a)?
What sort of information is available on files : Read the API documentation for the File class from the java.io package. What sort of information is available on files?
Reach optimum level of staff efficiency and guest service : Dawn Henlee, manager of the Hotel California, is considering how to restructure the front desk to reach an optimum level of staff efficiency and guest service. At present, the hotel has six clerks on duty, each with a separate waiting line, Determine..
Who is the mother of beatrice : CS-205: Declarative Programming (Prolog) Assignment. Translate the following questions into Prolog queries and try them out: Who is the mother of Beatrice? Who has a child (one or more)? Who is a desencendant of the Queenmother
Give an confidence interval for population mean home price : Suppose that the reported figure is a sample estimate based on 80 randomly chosen homes in this city, and that the population standard deviation was known to be $55,000. Give an 80% confidence interval for the population mean home price.
What is the process layout for such an operation : Research the process of producing an expensive product (assume that it is something that must cost at least $1,000).  What would you expect the layout of a producing facility to look like (in other words, what is the process layout for such an operat..
Confidence interval for the average amount of copper : Give a 95% confidence interval for the average amount of copper in the "population" of tons mined. Also give a 90% confidence interval and a 99% confidence interval for the average amount of copper per ton.
What information can factorial designs yield : Factorial designs allow researchers to study the effects of more than one independent variable simultaneously. Why is this advantageous? What information can factorial designs yield that nonfactorial designs cannot? The authors of your textbook s..

Reviews

len1372372

1/27/2017 12:49:08 AM

The work has to be the group’s own work. Deadline for solutions: See COS intranet Your solution is to be submitted electronically as two files. One for each question. (Please do not zip!) Use the CS-205 Assignments page on Blackboard for submission. Solution file format: .pl if accepted otherwise .txt. Embed results as comments in these files. Sufficient results should be given to test your code. Please register as a group on blackboard asap. Include group number, name(s) and student number(s) on the first page.

Write a Review

Programming Languages Questions & Answers

  Brief explanations for the solutions in the ms word file

You have a server-side script that cannot handle any ampersands (&) in the form data. Write a function that converts all ampersands in a form field to " and " when the field loses focus (onblur).

  Write program that calculate and prints the monthly paycheck

Write a program that calculates and prints the monthly paycheck for an employee. The net pay is calculated after taking the following deductions: Federal Income Tax: 15%.

  Program in perl that have two subroutines add() and multiply

Write a program in PERL that have two subroutines add() and multiply() that will add or multiply its arguments and print the result to screen.

  Define and create an exception

Quotient throws an exception to the higher-level function main to decide whether or not the program should be terminated. Main catches the divByZero and prints out an appropriate comment on the screen. Also include in main a try block to catch the..

  Write function to take the name of file as one parameter

Write a function named paragraphStats() that takes one parameter:the name of a file. The file contains a single line of text.

  Designing a solution for a programming problem

In your opinion, what are the three biggest challenges in planning and designing a solution for a programming problem

  Rewrite returns expression in which cond has been replaced

Write the function rewrite (x), x is an s-expr. If x is not a "legal" cond, rewrite returns nil. Otherwise, rewrite returns an expression in which each cond has been replaced by an equivalent sequence of nested if- then.

  Write program using to take input two integers by while loop

Write a program which uses while loops to perform following steps: Prompts user to input two integers: firstNum and secondNum. (firstNum must be less than secondNum).

  Design the array to do letter count for letters in alphabet

Design the array of 26 components to do letter count for 26 letters in alphabet and a variable for line count. (You may wish to call these variables letterCount and lineCount respectively.)

  Write a program that prompts for and reads an amount

Write a program that prompts for and reads an amount (integer) for Canadian dollars and convert the amount to Euros. The exchange rate is 1.

  Develop a program to input miles driven and gallons for tank

Develop a program in C++ that will input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful.

  Design architecture of system to accept natural language

Using the generic model of a language processing system presented here, design the architecture of a system that accepts natural language commands and translates.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd