Assignment n+1: Prolog Exercises

Due: Monday, 24 July 1995

This assignment is worth 5 of your final mark.

Syntax [5 marks]

Draw the terms corresponding to each of the following:
  1. trans((H:-true), (H))

  2. X=(match(X,Y):-call(X), Y>0)

  3. spaces --> [0' ], spaces ; []

  4. [a, [X] | (b, c)]

  5. + = '-' - 1

List Processing [15 marks]

Write Prolog predicates to perform the following list processing tasks. Each predicate must define everything it needs to run. I.e. do not define your predicate in terms of append, etc. In each case, precisely state the conditions required for the predicate to work e.g. ``at least one of L or M must be a proper list''. Provide a thorough set of test data, and sample output. For example:
%-- append( List1, List2, List3 )
%-- is true when List3 is the concatenation of List1 and List2.  The
%-- arguments can be variables, partially instantiated lists, or ground
%-- lists.
%--
%-- Test cases:
%--   append([1,2], [3,4], [1,2,3,4])
%--      should succeed
%--   append(A, [2,3], [1,2,3])
%--      should succeed, binding A to [1]
%--   append([1,2], [2,3], L)
%--      should succeed, binding L to [1,2,3,4]
%--   append([1,2], B, [1,2,3]
%--      should succeed, binding B to [3]
%--   append(A, B, [1,2]
%--      should give multiple solutions
%--          A = [], B = [1,2]
%--          A = [1], B = [2]
%--          A = [1,2], B = []
%--   append([], B, L)
%--      should unify B and L
%--   append(A, [], L)
%--      should unify A and L
  1. Write a predicate nextto(X, Y, List) that can be used to enumerate successive pairs from a list. E.g. nextto(X,Y,[1,2,3]) should generate the solutions X=1,Y=2 and X=2,Y=3. [5 marks]

  2. Write a predicate to spit a list into its even and odd elements. E.g. ?- even_odd([a,b,c,d],E,O) should bind E to [b,d] and O to [a,c]. [5 marks]

  3. Write a predicate to generate permutations of a list. E.g. perm([1,2,3], L) should (on backtracking) bind L to [1,2,3], [1,3,2], [2,1,3], etc. [5 marks]

Tic-Tac-Toe [15 marks]

You are to write a Prolog program to play tic-tac-toe (noughts-and-crosses). If you don't know how to play this game, send me some email and I will explain, The board will be represented as a term b/9, i.e. a term with b as the principle functor, and nine arguments. The arguments of this term will be either be `-' (for empty), `x' (for a cross) or `o' (for nought). The first three arguments are the top row of the board; the next three the middle row; and the last three the bottom row.
  1. Write a predicate defining the initial board. [1 mark]
  2. Write a predicate that tests if a tic-tac-toe board is a winning board for a given player. E.g. win(b(x,x,x,o,x,o,o,-,-),P) should succeed, binding P to x. [3 marks]

  3. Write a predicate to define legal moves in tic-tac-toe. The predicate will be given a player (x or o) and a board, and will return a new board and move number (e.g. 1--9). E.g. move(M, x, b(-,-,-,-,-,-,-,-,-), B1) should succeed, binding B1 to, say, b(x,-,-,-,-,-,-,-,-) and M to 1. [5 marks]

  4. Write a predicate play that generates all complete tic-tac-toe games. E.g.

    | ?- play( Moves ).
    Moves = [1,2,3,4,5,6,7] ;
    Moves = [1,2,3,4,5,6,8,7,9] ;
    Moves = [1,2,3,4,5,6,8,9,7] ;
    Moves = [1,2,3,4,5,6,9] ;
    Moves = [1,2,3,4,5,7,6,8,9] 
    etc.
    
    Note that boards like [1,2,3,4,5,6,7,8,9] are not generated, since there is an earlier win (to x in this case). [6 marks]

  5. How would you ask Prolog to look for a five-move game?