數值方法 100/1

(
) 1. 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.
(2) p :- c.
is p &lt;==&gt; (a &amp; b) V (~a &amp; c).
(
) 2. ‘not’ is a unary predicate and can be defined as not( P):- P, !, true; fail.
(
) 3. E1 =:= E2 is true if the term E1 and E2 are identical.
(
) 4. X is E is true if E matches the value of the arithmetic expression X.
(
) 5. The built-in predicate read is used for reading terms from the current input stream. The
predicate “read” is nondeterministic. If the goal read(X) fails, there will be
backtracking to input another term.
(
) 6. Program clauses should be short, but long procedures are acceptable if they have some
uniform structure.
(
) 7. The purpose of conforming to some stylistic conventions is to reduce the danger of
programming errors; and to produce programs that are readable and easy to understand
easy to debug and to modify
(
) 8 Program modification by assert and retract will degrade the transparency of the
program’s behavior.
(
) 9. There is no array facility in Prolog, but array can be simulated to some extent by using
the built-in predicates arg and functor.
(
) 10. Asserting intermediate results, also called “matching”, is a standard technique for
avoiding repeated computations.
(
) 11. If a dictionary has n nodes then its height should be logn.
(
) 12. A binary tree is either empty or it consists of three things: a root, a left subtree, and a
right subtree. The root can be anything, but the subtrees have to be binary tree again.
(
) 13. To search for an object in a binary dictionary, it is always sufficient to search at most
one subtree.
(
) 14. With cut we can often improve the efficiency of the program. However, in the
programs with cuts, a change in the order of clauses may affect the declarative
meaning.

1

(
(
(
) 1. Which answer is incorrect?
)
(A) | ?- name( ZX, [122, 120, 50, 51, 50]).
ANS: ZX = zx232
(B) | ?- integer( Z), Z = 2.
ANS: yes
(C) | ?- T =.. [rectangle, 5].
ANS: T = rectangle(5)
(D) |?- functor( t( f(X), X, t), Fun, Arity).
ANS: Arity = 1, Fun = t
2. Which one of the questions can obtain the answer ‘yes’ ?
(A) | ?- compound( 2 + X).
(B) | ?- f(a, X) = = f(a, Y).
(C) | ?- g(2,5) @&lt; f(3,4).
(D)| ?- f(a, X) =:= f(b, Y).
) 3. Given some facts as follows:
age( peter, 7).
age( ann, 5). age( pat, 8).
age( tom, 5).
The answer of the question | ?- bagof( Child, Age ^ age( Child, Age), List). is
(
(A) List = [5,7,8]
(B) List = [7,5,8,5]
(C) List = [ann,pat,peter,tom]
(D)List = [peter,ann,pat,tom]
) 4. Given a procedure as follows:
good_standard( jeanluis).
good_standard( francesco).
expensive( jeanluis).
reasonable( Restaurant) :- not expensive( Restaurant).
Which query will cause the following trace result?
11
22
33
44
44
33
53
53
22
11
(A)
(B)
(C)
(D)
(
Call: resonable(_16) ?
Call: not expensive(_16) ?
Call: '\$call'(expensive(_16),not,1,true) ?
Call: expensive(_16) ?
Exit: expensive(wangsteak) ?
Exit: '\$call'(expensive(wangsteak),not,1,true) ?
Call: fail ?
Fail: fail ?
Fail: not expensive(_16) ?
Fail: resonable(_16) ?
| ?- good_standard( X), reasonable( X).
| ?- reasonable( X), good_standard( X).
| ?- reasonable( X).
| ?- good_standard( X).
) 5. Which one is incorrect?
(A) “fail” is a goal that always fails, and “repeat” is a goal that always succeeds.
(B) “call( P)” invokes a goal P. It succeeds if P succeeds.
(C) With cut we can often improve the efficiency of the program.
(D) Using fail we can specify mutually exclusive rules.
2

1. (3%) What is ‘A tail-recursive procedure’?
2. (3%) Here shows a proceduce which can calculate N 3, N is a number.
cube :- read( N), C is N * N * N, write( C), cube.
Is the above proceduce correct? Why?
3. (3%) Given some facts as follows:
age( peter, 7).
age( ann, 5).
age( pat, 8).
age( tom, 5).
What is the meaning of quary ‘findall( Child, age( Child, Age), List).’?
4. (4%) 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
The goal squeeze would output:
The robot tried to pour wine out of the bottle.
squeeze :- get0( C), put( C) ,
.
dorest( 46) :- !.
dorest( 32) :- !, get( C), put( C), dorest( C).
dorest( Letter) :-
.
3
bottle.

5. (4%) Let add relation, add( X, L, L1), can add an item X to the list L only if X is not yet in L.
If X is already in L then L remains the same. Could you please define it by finishing the
following rule?
add( X, L, L) :- member( X, L),
.
).
6. (4%) Please define a generation procedure:
gen( N1, N2, List) which will, for two given
integers N1 and N2, produce the list List = [ N1, N1+1, N1+2, ..., N2].
gen( N, N,
).
gen( N1, N2,
7.
) :-
N1 &lt; N2, M is N1+1, gen(M, N2, List).
(6%) A procedure dosquares can read a sequence of numbers and outputs their squared.
| ?- dosquares.
3.
9 4.
16 10.
100 stop.
yes
Please finish this procedure by the following build-in predicates:
write, read, get, put, repeat, fail, !, true, not, call, end_of_file, tab, nl,
dosquares :-
(X = stop,
;
Y is X * X,write(Y),
).
8. (4%) Given a procedure as follows:
beat( tom, jim).
beat( ann, tom).
beat( pat, jim).
class( X, fighter) :- beat( X, _), beat(_, X), !.
class( X, winner) :- beat( X, _), !.
class( X, sportsman) :- beat(_, X).
(1) | ?- class( tom, sportsman).
(2) | ?- class( tom, C).
9. (3%) Given a procedure as follows:
writelist([]).
writelist([X|L]) :- write( X), tab(1), writelist( L).
(1) | ?- writelist( [a, b, c]).
4

10. (2%) The following procedure can find an item X in a binary dictionary.
in( X, t( _, X, _) ).
in( X, t( Left, Root, Right) ) :- gt( Root, X), in( X, Left).
in( X, t( Left, Root, Right) ) :- gt( X, Root), in( X, Right).
| ?- in( 5, D), in( 3, D), in( 8, D).
11. (6%) A procedure showfile can display on the terminal each term together with its consecutive
number.
| ?- showfile(3).
a.
3 a
aa.
4 aa
Please finish this procedure by the following build-in predicates:
write, read, get, put, repeat, fail, !, true, not, call, end_of_file, tab, nl,
showfile( N) :- read( Term), show( Term, N).
show(
, _ ) :-
.
show( Term, N) :- write(N), tab( 2), write( Term),
, N1 is N+1,
showfile( N1).
12. (4%) Define the procedure split( Numbers, Positives, Negatives)
which splits a list of numbers into two lists: positive ones (including zero) and negative ones.
For example: split( [3,-1,0,5,-2], [3,0,5], [-1,-2])
5

13. (6%) Consider a program to computer the Nth Fibonacci number for a given N. We can define a
predicate
forwardfib( M, N, F1, F2, F)
Here, F1 and F2 are the (M-1)st and the Mth Fibonacci numbers, and F is the Nth Fibonacci
fib(N,F) :- forwardfib(2, N, 1, 1, F).
forwardfib(M,N,F1, F2, F2) :- M &gt;= N.
forwardfib(M,N,F1, F2, F) :-
M &lt; N, NextM
, NextF
forwardfib(
,
).
14. (4%)Assume a reverse procedure:
reverse( List, ReversedList)
ReversedList has the same elements as List, but in the reverse order.
It can be defined as:
reverse([], []).
reverse([X |Rest], Reversed) :- reverse( Rest, RevRest), conc( RevRest, [X], Reversed).
Please transform it into a tail-recursive procedure.
15. (4%) A popular cryptarithmetic(密碼算術 ) puzzle procedure can be defined as follows.
However it includes 5 typo errors in the program. Please finish the following procedure.
sum(N1, N2, N) :- sum1( N1, N2, N, 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, Digs1, Digs2), del_var( D2, Digs2, Digs3), del_var( D, Digs3, Digs),
S
is
del_var( A, L, L) :-
, D is
, C is
, !.
del_var( A, [A|L], L).
del_var( A, [B|L], [B|L1]) :- del_var(A, L, L1).
% Some puzzles
puzzle1( [D,O,N,A,L,D], [G,E,R,A,L,D], [R,O,B,E,R,T] ).
6
.

16. (5%) Here is a quick sort program. Please correct five typo errors in the following program:
quicksort( List, Sorted) :- quicksort2( List, Sorted - [] ).
quicksort2( [ ], Z).
quicksort2( [X | Tail], A1 - Z2) :- split( X, Tail, Small, Big),
quicksort2( Small, A1 - A2 ), quicksort2( Big, A2 - Z2).
split( X, [ ], [ ], X).
split( X, [Y|Tail], Small, Big) :- gt( X, Y), !, split( X, Tail, Small, Big).
split( X, [Y|Tail], Small, Big) :- split( X, Tail, Small, Big).
17. (4%) The following procedure, makelist, can construct a properly ordered list of countries.
Germany, which has most neighbors in Europe, has to be put at the end of the list and other
countries have to be added at the front of the list. It starts the construction with some specified
country (Germany in our case) and collects the countries into a list called Closed. Each country
is first put into another list, called Open, before it is transferred to Closed. Each time that a
country is transferred from Open to Closed, its neighbors are added to Open. However the
following program includes 5 typo errors. Can you correct it?
makelist( List):- collect( [germany], [], List).
collect([], Open, Closed).
collect([ X | Open], Closed, List):- member( X, Open),!, collect( Open, Closed, List).
collect([ X | Open], Closed, List):- ngb( X, Ngbs), conc( Ngbs, Open, Open1),
collect( Open, Closed, List).
18. (7%) The del( D1, X, D2) procedure can delet a node X from a binary dictionary D1, and the
result is shown in D2. Please correct seven typo errors in the following program:
del( t( nil, X, Right), X, Right).
del( t( Left, X, nil), X, Left).
del( t( Left, X, Right), X, t( Left, Y, Right1)) :- delmin( Right, Y, Right1).
del( t( Left, Root, Right), X, t( Left, Root, Right)) :gt( Root, X), del( Left, X, Left).
del( t( Left, Root, Right), X, t( Left, Root, Right)) :gt( X, Root), del( Right, X, Right).
delmin( t( nil, Y, R), [], []).
delmin( t( Left, Root, Right), Y, t( Left1, Y, Right)) :- delmin( Left, Y, Left1).
7