###### tags: `prolog` # Promax 107 Final 一、 是非題(12%) ( <font color="#f00">X</font> ) 1. atomic( X) is false if X currently stands for a number or an atom. ( <font color="#f00">X</font> ) 2. ‘not’ is a unary predicate and can be defined as not( P):- P, !, true; fail. ( <font color="#f00">O</font> ) 3. In the programs with cuts, a change in the order of clauses may affect the declarative meaning. For example, the declarative meaning of the clauses (1) p :- a, !, b, d. (2) p :- c. is p <==> (~a & c) V (a & b & d). ( <font color="#f00">X</font> ) 4.The predicate “read” is non-deterministic. If the goal read( X) fails, there will be backtracking to input another term. ( <font color="#f00">O</font> ) 5. X @< Y is true if term X precedes term Y. The precedence between structures is determined by the precedence of their principal functors. ( <font color="#f00">X</font> ) 6. ‘=..’ is a built-in predicate. The goal ‘Term =.. L’ is true if Term is a list that contains the principal functor of L, followed by its arguments. ( <font color="#f00">O</font> ) 7. A goal asserta( C) always succeeds and causes a clause C to be added to the database. And a goal retract( C) deletes a clause that matches C. ( <font color="#f00">X</font> ) 8. Red cuts had no effect on the declarative meaning, and green cuts do affect the declarative meaning of the programs. ( <font color="#f00">O</font> ) 9. E1 =:= E2 is true if the values of the arithmetic expressions E1 and E2 are equal. 二、 單選題(12%) ( <font color="#f00">D</font> ) **1. Given four queries as follows: | ?- asserta(age( peter, 7)). | ?- assertz(age( ann, 5)). | ?- assertz(age( pat, 8)). | ?- asserta(age( tom, 5)).** **The answer of the question | ?- bagof( Age, Child^age( Child, Age), List). is** >>(A) List = [5, 7, 8] (B) List = [7, 5, 8, 5] \(C\) List = [5, 5, 7, 8] (D) List = [5,7,5, 8] ( <font color="#f00">D</font> ) **2. Which one of the questions can obtain an answer including ‘yes’?** >>(A) | ?- compound( ‘2 + X’). (B) | ?- f(a, X) =:= f(a, Y). \(C\) | ?- atomic( 2 + X). (D) | ?- f(a, X) = f(a, Y). ( <font color="#f00">A</font> )**3. Given a program as follows: good_standard( wangsteak). good_standard( tasty). expensive(wangsteak). reasonable( Restaurant) :- not(expensive( Restaurant)). Which one of the following questions can obtain only one answer ‘X = tasty.’ ?** >>(A) good_standard( X), reasonable( X). (B) reasonable( X), good_standard( X). \(C\) not(expensive( X)). (D) good_standard( X). ( <font color="#f00">B</font> ) **4. Which one is the answer of the question | ?- write(‘test’). ?** >>(A) test. (B) test \(C\) [116,101,115,116] (D) syntax_error ( <font color="#f00">D</font> ) **5. Which answer of the following question is ‘no’?** >>(A) | ?- name(123, [49,50, 51]). (B) | ?- Z=2, integer( Z). \(C\)| ?- arg(2, f( t(a), b, t(b)), b). (D) |?- functor( t( f(X), X, t2), t, 2). ( <font color="#f00">B </font> ) **6. Which one is incorrect?** >>(A) “fail” is a goal that always fails, and “true” is a goal that always succeeds. (B) “repeat” will repeat a goal and it always fails. \(C\) “cut” (!) prevents backtracking. With “cut” we can often improve the efficiency of the program. (D) There is no array facility in Prolog, but array can be simulated to some extent by using the built-in predicates arg and functor. 三、 簡答題 1. (3%) A procedure **dosquares** can read a sequence of numbers and outputs their squared **| ?- dosquares. 3. 9 4. 16 stop. yes** Please finish this procedure by the following build-in predicates: **write, read, get, put, repeat, fail, cut, true, not, call, asserta, assertz, retract, setof, bagof, dosquares :- <font color="#f00">true</font> , read(X), (X = stop, <font color="#f00">!</font> ; Y is X * X, write(Y), <font color="#f00">dosquares</font> ).** 2. (2%) squeeze can read a sentence from the current input stream, and output the same sentence reformatted so that multiple blanks between works are replaced by single blanks. For example: An acceptable input is then: **The robot tried to pour wine out of the bottle.** The goal squeeze would output: **The robot tried to pour wine out of the bottle.** Please correct two typo errors in the following program. **squeeze :- <font color="#f00">get0( C)</font>, put( C), dorest( C). dorest( 46) :- !. dorest( 32) :- !, get( C), put( C), dorest( C). dorest( C) :- dorest( C),<font color="#f00">squeeze</font>.** 3. (2%) A procedure **maketable** can generate a table of products of all pairs of integers between 1 and 9. Please finish this procedure. **maketable :- L = [1,2,3,4,5,6,7,8,9], member( X, L), member( Y, L), Z is X * Y, <font color="#f00">asserta</font>( product( X, Y, Z)),<font color="#f00">fail</font>.** 4. (1%) Given a procedure as follows: **fun( N, N, [N]). fun( N1, N2, [N1|List]) :- N1 < N2, M is N1+1, fun(M, N2, List).** Please answer the question: | ?- **fun (3,8,L).** <font color="#f00">**L = [3,4,5,6,7,8] yes**</font> 5. (1%) Given a procedure as follows: **list1([]). list1([L|LL]) :- line1( L), nl, list1( LL). line1([]). line1([X|L]) :- write( X), tab(2), line1( L).** Please answer the question | ?- **list1([[a],[b,c],[d,e,f]]).** <font color="#f00">**a b c d e f yes**</font> 6. (3%) Define the relation **substitute( Subterm, Term, Subterm1, Term1)** if all occurrences of Subterm in Term are substituted by Subterm1 then we get Term1. For example: | ?- substitute( sin(x), 2*(sin(x)*f(sin(x)), t, F) **F = 2 * t * f(t)** The procedure, substitute, can be defined as follows. Please finish this procedure. **substitute( Term, Term, Term1, Term1) :- !. substitute( _, Term, _, Term) :- <font color="#f00">atomic(Term)</font>, !. substitute( Sub, Term, Sub1, Term1) :- Term =.. [F|Args], substlist( <font color="#f00">Sub,Args,Sub1,Args1</font>), Term1 =.. [F|Args1]. substlist( _, [], _, []). substlist( Sub, [Term|Terms], Sub1, [Term1|Terms1]) :- substitute( Sub, Term, Sub1, Term1), <font color="#f00">substlist</font>( Sub, Terms, Sub1, Terms1).** 7. (5%) Define procedure sum: **sum( N1, N2, N3)** where N1, N2 and N3 represent the three numbers of a given cryptarithmetic puzzle. For example: | ?- **puzzle1(N1, N2, N3), sum( N1, N2, N3). N1 = [5,2,6,4,8,5] N2 = [1,9,7,4,8,5] N3 = [7,2,3,9,7,0]? ; no** The procedure, sum, can be defined as follows. Please fill the blanks. **sum(N1, N2, N3) :- sum1( N1, N2, N3, 0, 0, [0,1,2,3,4,5,6,7,8,9], _). sum1( [ ], [ ], [ ], C, C, Digits, Digits). sum1( [D1|N1], [D2|N2], [D|N], C1, C, Digs1, Digs) :- sum1( N1, N2, N, C1, C2, Digs1, Digs2), digitsum( D1, D2, C2 , D, C, Digs2, Digs). digitsum( D1, D2, C1, D, C, Digs1, Digs) :- del_var(D1, <font color="#f00">Digs1</font> , Digs2), del_var( D2, Digs2, Digs3), del_var(D, Digs3, Digs), S is <font color="#f00">D1+D2+C1</font>, C is <font color="#f00">S // 10</font>, D is <font color="#f00">S mod 10 </font> . del_var( A, L, L) :- nonvar(A), !. del_var( A, [A|L], L). del_var( A, [B|L], [B|L1]) :- del_var(A, L, L1). puzzle1( [D,O,N,A,L,D], [G,E,R,A,L,D], [R,O,B,E,R,T] ).** 8. (5%) Here is a program to transform a sentence into a list of atoms. For example: | ?- **getsentence(X). Mary was pleased to see the robot fail. X = ['Mary',was,pleased,to,see,the,robot,fail] yes** Please find five typo errors in the following program: **getsentence( Wordlist) :- ~~get~~( Char), getrest( Char, Wordlist). getrest(46, [] ) :- !. getrest(32, Wordlist) :- !, getsentence( Wordlist). getrest( Letter, [Word | Wordlist] ) :-getletters( ~~Letters~~, ~~Letter~~, Nextchar), ~~member~~( Word, Letters),getrest( Nextchar, Wordlist). getletters( 46, [], 46) :- !. getletters( 32, [], 32) :- !. getletters( Let, [Let | Letters], Nextchar) :-~~get~~( Char), getletters( Char, Letters, Nextchar).**