CSE 341 -- Assignment 8 -- CLP(R)

Due in lecture Dec 9, 1998
No late assignments accepted this time, since we want to post the solution right away. Paper turnin only (no electronic turnin).

  1. You don't need to hand in anything for Question 1 -- this is just to give you some practice.

    Define the append and sum relations as follows:

       append([],L,L).
       append([H|T],L,[H|U]) :- append(T,L,U).
    
       sum([],0).
       sum([X|Xs],X+S) :- sum(Xs,S).
    
    Try append on the following. In each case reject the answers to see what happens when CLP(R) backtracks.
       append([a,b,c],[w,x,y,z],L).
       append([a,b],Y,[a,b,c,d]).
       append([a,c],Y,[a,b,c,d]).
       append(X,Y,[a,b,c,d]).
       append(X,Y,Z).
    
    Now try sum on the following, again backtracking when possible.
       sum([5,10,20],N).
       sum([5,10,20],8).
       sum([5,X],100).
       sum([5,X,Y],100).
       sum(A,100).
    

  2. Exercise P4.3 from Marriott and Stuckey (define the abs predicate). Show your predicate working with various combinations of constants and variables as arguments, including both arguments constants, both variables, and one constant and one variable. Backtrack to find multiple answers.

  3. Exercise P6.3 from Marriott and Stuckey (finding temperature values).

  4. Define a rule listmax that finds the maximum element of a list of numbers. For example:
    listmax([10,5,8],X)  succeeds with X=10
    listmax([A,5,8],100) succeeds with A=100
    listmax([A,5,8],5)   fails
    
    Try your rule on the above cases, and also listmax([A,B,C],100) and listmax(A,100). (Backtrack to find all of the answers, or if there are an infinite number, the first several answers that are produced.)