數值方法

advertisement
第1頁共7頁
邏輯語言程式設計(A 版)
系級:
座號:
姓名:
99/11
一、 是非題(20%)
(
) 1. Prolog clauses consist of head and body. Facts are clauses and only have the body and
they declare things that are always true.
(
) 2. If X15 occurs in two clauses, then it signifies two different variables. But each
occurrence of X15 with in the same clause means the same variables.
(
) 3. Operators with highest precedence bind strongest.
(
) 4. Atoms and numbers are constants. Variables belong to simple objects.
(
) 5. In Prolog, point( X, Y) and point( X, Y, Z) are the same functors.
(
) 6. In Prolog, matching, if it succeeds, results in the most general instantiation of variables.
(
) 7. If not is defined as fx then not not p is illegal.
(
) 8. A rule P :- Q; R, S. means that P is true if (Q is true) or (R and S is true).
(
) 9. 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.
(
) 10. An automation is said to accept the input string if there is a transition path in the graph
such that it ends with any of its states.
二、 選擇題(8%)
(
)
(
(
1. Which one is not an atom?
(A) .:.
(B) ‘S_A’
)
)
(C) _35ab
(D) <--->
2. Which list can not be accepted by Prolog?
(A) [ _, X, _ | Other]
(B) [Item1 | Item2, Item3]
(C) [ _ | [Tail]]
(D) [[Item1] | _ ]
3. Which one of the answers is ‘yes’ in Prolog?
(A) 1+2 =:= 2+1. (B) 3 = 2+1.
(C) 1+2 =/= 2+1.
(D) 2+1 is 3.
(
)
4. Please finish the predicate sumlist( List, Sum) so that Sum is the sum of a given list
of numbers List.
length( [], 0).
length( [ X | Tail], S1) :- length( Tail, S2),
.
(A) S2 is S1 +1
(B) X is S1 + S2
(C) S2 is S1 +X
(D) S1 is S2 +X
(
)
5. Please finish the membership relation: member( X, L) where X is an object and L is
list.
member(
).
member( X, [ _ | Tail]) :- member( X, Tail).
(A) [ ], [ ]
(B) X, [X| _ ]
(C) Tail, [ _ | Tail]
(D) X, X
1
第2頁共7頁
三、 簡答題
1. (7%) Given the following facts, please answer the questions.
parent( pam, bob).
% Pam is a parent of Bob
parent( tom, bob).
parent( tom, liz).
parent( bob, ann).
parent( bob, pat).
parent( pat, jim).
predecessor( X, Z) :- parent( X, Z).
predecessor( X, Z) :- parent( X, Y), predecessor( Y, Z).
(a) (4%) Please define a rule “grandchild(X, Y)”. It means that X is a grandchild of Y.
(b) (3%) Please draw the corresponding derivation diagrams.
?- predecessor( pam, pat).
2. (4%) Consider the following program:
f( 1, one).
f( s(1), two).
f( s(s(1)), three).
f( s(s(s(X))), N) :- f( X, N).
How the Prolog answer the following questions?
(a) |?- f( s(1), A).
(b) |?- f( s(s(s(s(s(s(s(1))))))), C).
2
第3頁共7頁
3. (6%) (1) Please define the concatenation relation: conc( L1, L2, L3) here L1 and L2 are two
lists, and L3 is their concatenation.
For example: conc( [a, b], [c, d], [a, b, c, d]) is true
conc( [a, b], [c, d], [a, b, a, c, d]) is not true
(2) Please define the sublist relation: sublist( S, L) by using conc relation. This relation has two
arguments, a list L and a list S such that S occurs within L as its sublist.
For example: sublist( [c, d, e], [a, b, c, d, e]) is true
sublist( [c, e], [a, b, c, d, e, f]) is not true
4. (4%) Define the relation reverse(List, ReversedList) that reverses lists. For example,
reverse([a, b, c, d], [d, c, b, a]).
5. (4%) Define the predicate palindrome( List). A list is a palindrome(迴文) if it reads the same in
the forward and in the backward direction. For example, [m,a,d,a,m].
6. (4%) Define the predicate ordered( List) which is true if List is an ordered list of numbers. For
example: ordered([1,5,6,6,9,12])
3
第4頁共7頁
7. (4%) Given a program as
follows:
big( bear).
big( elephant).
small( cat).
brown( bear).
black( cat).
gray( elephant).
dark(Z):- black(Z).
dark(Z):- brown(Z).
Please show all the goal list when the program answer the
following question:
| ?- dark(X), big(X). (goal list: dark(X), big(X))
1 1 Call: dark(_16) ?
2 2 Call: black(_16) ? (goal list:
)
2 2 Exit: black(cat) ?
1 1 Exit: dark(cat) ?
3 1 Call: big(cat) ?
3 1 Fail: big(cat) ?
1 1 Redo: dark(cat) ?
2 2 Call: brown(_16) ?
2 2 Exit: brown(bear) ? (goal list:
)
1 1 Exit: dark(bear) ?
3 1 Call: big(bear) ?
3 1 Exit: big(bear) ?
X = bear
yes
8. (6%) Here is a program to solve the monkey and banana problem. 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; and (4) Monkey does not have banana.
Please finish the following program:
move( state(
), grasp, state( middle, onbox, middle, has) ).
move( state( P, onfloor, P, H), climb, state(
) ).
move( state( P1, onfloor, P1, H), push( P1, P2), state(
move( state( P1, onfloor, B, H), walk( P1, P2), state( P2, onfloor, B, H) ).
canget( state( _, _, _, has) ).
canget( State) :- move( State, _, State1), canget( State1).
4
) ).
第5頁共7頁
9. (14%) Given a finite atuomaton as follows:
b
a
c
s1
s2
s5
s6
a
b
b
b
null
null
s4
b
s8
s3
a
s7
(1) (2%) A unary relation final which defines the final states of the automaton. Please show two
of the final facts.
ANS:
(2) (2%) A three-argument relation trans which defines the state transitions so that
trans( S1, X, S2) means that a transition from a state S1 to S2 is possible when the current
input symbol X is read. Please show two of the trans facts.
ANS:
(3) (2%) A binary relation silent( S1, S2) meanings that a silent move is possible from S1 to S2.
Please show two of the silent facts.
ANS:
(4) (6%) Please define the acceptance of a string from a given state:accepts( State, String)
(A) The empty string, [], is accepted from a state State if State is a final state.
accepts( State, []):(B) A non-empty string is accepted from State if reading the first symbol in the string can
bring the automaton into some state State1, and the rest of the string is accepted from
State1.
accepts( State, [ X| Rest]):(C) A string is accepted from State if the automaton can make a silent move from State to
State1 and then accept the (whole) input string from State1.
accepts( State, String):(5) (2%) What kind of strings can be accepted by this automaton?
5
第6頁共7頁
10. (4%) 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.
Given the del relation as follows:
del( X, [X| Tail], Tail).
del( X, [Y| Tail], [Y|Tail1]) :- del( X, Tail, Tail1).
Please use the del relation to define the insert and member relations.
(1) Inserting X at any place in some list List giving BiggerList can be defined:
insert( X, List, BiggerList) :(2) The permutation relation:
permutation([ ],[ ]).
permutation(L, [ X| P]):-
11. (6%) Please show the output of the following questions.
(1) length1( [ ], 0).
length1( [ _ | Tail], N) :- length1( Tail, N1), N = 1 + N1.
| ?- length( [a, b, [c, d], e], N)
(2) | ?- 1+A = B+2.
(3) | ?- date( D, M, 2001) = date(D1, may, Y1).
6
第7頁共7頁
12. (9%) If a family sturcture of a database defined as follows:
(1) Each family has three components: husband, wife, and children.
(2) The children are represented by a list.
(3) Each person represented by a structure of four components: name, surname, date of birth,
and job.
(4) The job information is ‘unemployed’, or it specifies the working organization and salary.
An example of a family structure of a database is shown as follows:
family(
person( tom, fox, date(7,may,1960), works( bbc, 15200)),
person( ann, fox, date(9,may,1961), unemployed),
[ person( pat, fox, date(5,may,1983), unemployed),
person( jim, fox, date(5,may,1983), unemployed) ] ).
If some useful utility procedure for the database are defined:
husband( X) :- family( X, _, _).
wife( X) :- family( _, X, _).
child( X) :- family( _, _, Children), member( X, Children).
exists( Persons) :- husband( Persons); wife( Persons); child( Persons).
dateofbirth( person(_, _, Date, _), Date).
salary( person(_, _, _, works(_, S)), S).
salary( person(_, _, _, unemployed), 0).
(1) (2%) Define the relation twins(Child1, Child2) to find twins in the family database.
(2) (3%) Please define the total relation which will sum the total income of a given family.
total(
).
total( [Person | List], Sum) :- salary(Person, S), total(
(3)
(2%) Please use these utilities in the following query to the database:
Names of families without children.
(4) (2%) Please show the meaning of the following query.
wife( person( Name, Surname, _, works(_, _))).
7
),
.
Download