Homework 3: Logic Programming



1. Write a predicate called same_generation(X,Y) which succeeds if individuals X and Y are cousins of the same generation (i.e., the are at the same level in the family tree).


2. Program the logical definition of plus and times given in class. Define the factorial and fibonacci functions using logical arithmetic. Draw the search tree for the query:

?- factorial(X, (s(s(0)))).


3. Write a predicate sumlists(List1, List2, List3, Out) that takes three lists of integers, List1, List2, and List3 and computes their sum in Out. For example, sumlists([1,2,3],[3,4,5],[1,1,1],X) should return X = [5,7,9]. Try to handle the case where the three lists are of different lengths (the output list would be the length of the longest input list.)


4.  Program the Tower of Hanoi puzzle.


5.  Suppose we represent sets (no duplicated elements) ranging over integers as lists of integers. Write Prolog predicates for performing the following set operations:

union(S1,S2): returns the set-union of sets S1 and S2.

intersection(S1,S2): returns set-intersection of sets S1 and S2.

setdiff(S1,S2): returns the set-difference of sets S1 and S2

subset(S1,S2): returns true if S1 is a subset of set S2, false otherwise.


6.  Program SEND+MORE=MONEY in Prolog; 


7.  Write a Prolog program for performing merge sort.


8. Write a program that will draw an iscosceles triangle whose base is of size N on the screen. Assume N is an odd natural number greater than 1.