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/3 and times/3 given in class. Define the factorial/2 and fibonacci/2 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]. Your program should 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: hanoi(Num_Disks, Peg1, Peg2, Peg3, List_of_Moves)


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,Sout): Sout is set-union of sets S1 and S2.

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

setdiff(S1,S2,Sout): Sout is set-difference of sets S1 and S2.

subset(S1,S2): succeeds if S1 is a subset of set S2.

powset(S,Sout): Sout is powerset of S.


6.  Program SEND+MORE=MONEY in Prolog:  solve([S,E,N,D,M,O,R,Y]).


7.  Write a Prolog program for performing merge sort:  mergesort(Lin,Lout).


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: draw_isoc(Number). See example output here.


BONUS PROBLEM (you get extra credit for solving it)

Write a general Prolog program for solving cryptarithmetic puzzles such as SEND+MORE=MONEY, AM+PM=DAY, LIVE+VILE=EVIL: Call the predicate gsolve(L1,L2,L3) where L1, L2 are the two top rows of numbers represented as lists, and L3 is the sum row.