Download 1. Logic Programming and Prolog: A First Glimpse

Transcript
Logic Programming and Prolog
21
______________________________________________________________
We can ask the following questions:
(a) What is the population density of Switzerland?
?(b) Are there countries with a population density less than 50 persons per square kilometer?
?(c)
What countries are smaller than Switzerland?
?-
Let us write one more predicate:
% bigger_than(A) :- there is a country bigger than A square km
How can we find the biggest country?
?-
2. Let us compute the number of completely balanced binary trees having N nodes. A binary
tree is completely balanced if for any node the following property holds: the number of nodes
in its left subtree and the number of nodes in its right subtree differ by not more than 1.
% ncbbt(N,T) :- T is the number of completely
%
balanced binary trees having N nodes