邏輯語言程式設計(A) 學號: 姓名: 97/11 一、 是非題( 40%) ( ) 1. A procedure is a set of clauses about the same relation. ( ) 2. The lexical scope of variable names is one clause. Thus the same variable name in two clauses means two different variables. ( ) 3. Facts are clauses that have a head and the empty body. Questions only have the body. Rules have the head and the (non-empty) body. ( ) 4. Operators can be infix, prefix, or postfix. ( ) 5. If not is defined as fx then not not p is illegal. ( ) 6. Prolog clauses are of three types: facts, rules and questions. Rules declare things that are always, unconditionally true. ( ) 7. The procedural meaning determines what will be the output of the program, and the declarative meaning determines how this output is obtained. ( ) 8. The declarative meaning does depend on the order of goals and clauses. Thus the order can affect the efficiency of the program; an unsuitable order may even lead to infinite recursive calls. ( ) 9. A comma (,) between goals means that disjunction of goals. A semicolon (;) between goals means the conjunction of goals. ( ) 10. Operators with highest precedence bind strongest. 二、 簡答題( 60%) 1. (5%) Consider the program: t(0+1, 1+0). t(X+0+1, X+1+0). t(X+1+1, Z) :- t(X+1, X1), t(X1+1, Z). How will this program answer the following questions if “+” is an infix operator of type yfx ? | ?- t(1+0+1+0+1, C). 2. (5%) Please show the output of the following program: abc( [], L, L ). abc( [X| L1], L2, [X| L3]) :- abc( L1, L2, L3). |?- abc([a, b], [a, b, [b,c] ], X). 3. (8%) Given the program as follows: parent( pam, bob). parent( tom, bob). parent( bob, ann). parent( bob, pat). (1) Please define the offspring (子女) relation. (2) Please define the grandparent (祖父毋) relation. parent( tom, liz). parent( pat, jim). 4. (18%) Here is a program to solve the monkey and banana problem. Please find six typo errors in the following program: move( state( middle, onbox, middle, hasnot), grasp, state( middle, onbox, middle, hasnot) ). move( state( P, onbox, P, H), climb, state( P, onbox, P, H) ). move( state( P1, onfloor, P1, H), push( P1, P2), state( P2, onfloor, P2, H) ). move( state( P1, onfloor, P1, H), walk( P1, P2), state( P2, onfloor, P2, H) ). canget( State( _, _, _, has) ). canget( State1) :- move( State1, Move, State2), canget( State1). PS. The initial state is state( atdoor, onfloor, atwindow, hasnot). It means (1) Monkey is at door. (2) Monkey is on floor. (3) Box is at window. (4) Monkey does not have banana. 5. (24%) Please finish the following procedures: The membership relation: member( X, L) where X is an object and L is list. member( X, [X| Tail]). member( X, [ | Tail]) :- member( ). Deleting an item X form a list L can be programmed as a relation: del(X, L, L1) where L1 is equal to the list L with the item X removed. del( ). del( ) :- del( X, Tail, Tail1). The permutation relation: prmutation([],[]). permutation(L, [ X| P]):- del( ), permutation( ). GCD (greatest common divisor) problem: Given two positive integers, X and Y, and their greatest common divisor, D, can be found. gcd( ). gcd( X, Y, D) :- X<Y, , gcd( X, Y1, D). gcd( X, Y, D) :- Y<X, gcd( Y, X, D). Length counting problem: length( List, N) which will count the elements in a list List and instantiate N to their number. length( [], 0). length( [_ | Tail], N) :- length( Tail, N1), .