$30
Exercise 1. Looping
countDown(N)- repeat, writeln(N), N is N-1, N0, !.
Test the above predicate with the query - countDown(5).. Explain what happens and why Find a solution to the problem that you have observed.
Exercise 2. Chemical Elements
Given the following database of chemical elements and their symbols in the periodic table. For example
element(chlorine,'Cl').
element(helium,'He').
element(hydrogen,'H').
element(nitrogen,'N').
element(oxygen,'O').
Write an interactive Prolog program for the user to enter a symbol and the program prints the name of the corresponding chemical element. If the symbol is not in the database, the program should exit otherwise loop again. Example
Elements in the Periodic Table
Symbol to look-up 'O'.
O is the symbol for oxygen
Symbol to look-up 'He'.
He is the symbol for helium
Symbol to look-up 'K'.
Don't know the symbol K
Exiting.
Exercise 3 Cut-Fail
Consider the following database
canalOpen( saturday ).
canalOpen( monday ).
canalOpen( tuesday ).
raining( saturday ).
melting( saturday ).
melting( sunday ).
melting( monday ).
Write a Prolog predicate to return true if it is advisable to go to winterlude. It should return true if the Canal is open, it is not raining and not melting You must use cut-fail in your solution and your predicate should work as follows
- winterlude(X).
X = tuesday.
Hint You will need to add an intermediate predicate to deal with negation.
Exercise 4. List Processing
Write a Prolog predicate secondLast that finds the second last element of a list.
- secondLast(X,[7,abc,foo,x,9,12]).
X = 9
Exercise 5 and Quiz Lists
Write a predicate which processes a list of numbers by adding up every second number. The predicate is to work as shown below. (Note that you are not asked to check the input to be a valid list of numbers but can simply assume that your predicate always receive list of numbers).
- addSecond([1,3,5,4],S).
S = 7.
- addSecond([1,3,5],S).
S = 3.
- addSecond([1,3],S).
S = 3.
- addSecond([1],S).
S = 0.
- addSecond([1,3,5],3).
true.