minmax(X,X,[X]). minmax(Min,Max,[X|L]) :- minmax(Min2,Max2,L), min(X,Min2,Min), max(X,Max2,Max). min(X,Y,X) :- X =< Y. min(X,Y,Y) :- Y =< X. max(X,Y,X) :- Y =< X. max(X,Y,Y) :- X =< Y.
Write a predicate travel/2 which determines whether it
is possible to travel from one place to another by ‘chaining together’
car, train, and plane journeys. For example, your program should answer
yes to the query travel(valmont,raglan).
So, by using travel/2 to query the above database, you
can find out that it is possible to go from Vamont to Raglan. In case
you are planning a travel, that’s already very good information, but
what you would then really want to know is how exactly to get from
Valmont to Raglan.
Write a predicate travel/3 which tells you how to travel
from one place to another.
Now write a 3-place predicate combine2 which takes
three lists as arguments and combines the elements of the first two
lists into the third as follows:
Result = [[foo,glub],[bar,glab],[yip,glib],[yup,glob]]
Finally, write a 3-place predicate combine3 which takes
three lists as arguments and combines the elements of the first two
lists into the third as follows:
Write a Prolog program to implement the predicate
frequencies(L,M), which takes as input a list
L and returns a list of pairs [x,n], where
x is an element of L and n is the
number of times x appears in L. Here are some
sample runs in SWI Prolog:
Assume that a binary tree is represented in Prolog using the function
term
1
tree(X,LeftSubTree,RightSubTree)
where X is the element at any node and
LeftSubTree and RightSubTree are the left
sub-tree and the right sub-tree for the node respectively. The term nil
is used when there is no left sub-tree or right-tree. For example, the
following function term:
would represent the following binary (search) tree:
1 2 3 4 5
20 / \ 10 40 / 30
Recall that a binary search tree is a binary tree in which all the
elements in the left sub-tree of a node are less than the element at the
node and all the elements in the right sub-tree of a node are greater
than the element at the node. Assume that the binary search tree is
being used to represent a set of numbers with no duplicates. Write
Prolog programs for the following predicates:
smallest(X,T) is true if X is the smallest
element in the binary search tree T.
member(X,T) is true if X is an element in
the binary search tree T.
height(T,H) is true if H is the height of
the binary search tree T where height is defined as the
length of the longest path from root to leaf node.
% WRONG height(nil,0). height(tree(_,nil,nil),0). height(tree(_,LeftSubTree,RightSubTree),H):- height(LeftSubTree,HL), height(RightSubTree,HR), H is max(HL,HR)+1.