Practice Problems
AM Session Slides HERE:
Room#:
1. Write a prolog program to split a number of seconds the hours, minutes, and seconds.
hms(TotalSecs, Days, Hours, Mins, Secs) It should work as follows:
?- hms(130, D, H, M, S). D=0,
H=0
M=2
S=10
?- hms(T, 4, 12, 10, 15).
7200 ; On my little calculator 7200 = ((4*24 +12)*60 + 10)*60 +15
2. How many
a. Clauses? 0 facts + 1 rule = 1 clause
b. Facts? 0 (no neck)
c. Rules?1 (neck) :-
d. Predicates? 1 hms/2
e. Variable Terms? 7
f. Constant Terms?
hms(T, D, H, M, S) :-
S is T mod 60,
TM is T // 60,
M is TM mod 60,
TH is TM // 60,
H is TH mod 24,
D is TH // 24.
hms(0, 0,0,0).
+ – –
*
/
// mod **
Room#:
1. In prolog, data structures are represented as clauses.
For example, we can represent books using the structure book(Title, Author).
And authors can be represented by the structure author(FirstName, LastName).
a. Write a fact owns/2 that indicates that James owns a book named ‘Big Fish’ by an author named Emily Schue.
b. Write a fact that indicates that Julia owns a book by an author whose first name is Isaac.
c. Write a rule similar/2 that takes two names, and is true if the two named people own books by the same author.
owns(‘James’, book(‘Big Fish’, author(‘Emily’, ‘Schue’))). owns(‘Sam’, book(‘Small Fish’, author(‘Emily’, ‘Schue’))).
% Write a fact that indicates that Julia owns a book by an author whose first name is Isaac.
owns(‘Julia’, book(_, author(‘Isaac’, _))).
% Write a rule similar/2 that takes two names, and is true if the two named people own books by the same author.
similar(N1, N2) :- owns(N1, book(_, A1)), owns(N2, book(_, A1)), N1 \= N2.
Room#:
1. Assume a structure point(X, Y) exists and write the following rules.
a. A point is above/2 another point if its Y coordinate is less
b. A point is below/2 if …. You get the idea!
c. Write a rule left/2
d. Write a rule right/2
2. Assume a structure line(P1, P2) exists, where P1 and P2 are points.
a. Write a rule horizontal/1 that is true of a line is horizontal. That is, both points have the same Y coordinate.
b. Ditto for vertical
c. Write a rule length/2 that accepts a line as its
first argument and computes the length of the line in the second argument.
So you don’t mess up the code, start by writing out the formula for the distance between two points.
+x axis
0 5 10 15 0
5
10
+y axis
Room#:
a + b + c+ d
Room#:
a. abcd – no b.
c.
d. accd
S aScB aAcB accB accd Yes!
e. ccc
S→ A → cA → ccA → ccc
Room#:
S→ A SbA — or — S → A | AbS A → aA | abA
Room#:
Room#:
In section 3.4.5, the rules are:
● Types can be int or real
● On the RHS, if both types are int then the type of the
expression is int, otherwise real.
● The type on the LHS must match the type on the RHS
● Attributes are expected_type and actual_type.
Room#:
Using the VM described in the chapter (statements on the left), write an operational semantics definition of…..
a. Java do-while loop
b. A for loop with this syntax: for Var in 1 .. N by Step do L done.
c. A Java if-then-else.
Room#:
Using denotational semantics, describe the meaning Mb of a bin_num as shown in the grammar below.
Room#:
Using denotational semantics, describe the meaning Moct of a oct_num as shown in the grammar below.
Room#: 2
1. Write a prolog program to split a number of seconds the hours, minutes, and seconds.
hms(TotalSecs, Hours, Mins, Secs) It should work as follows:
?- hms(130, D, H, M, S). D=0,
H=0
M=2
S=10
?- hms(T, 4, 12, 10, 15).
7200 ; On my little calculator 7200 = ((4*24 +12)*60 + 10)*60 +15
Integer division with //
hms(TotalSecs,Hours, Mins, Secs) :- Hours is TotalSecs // 60 // 60, HoursInSecs = Hours * 60 * 60,
Mins is (TotalSecs – HoursInSecs) // 60, MinsInSecs is Mins * 60,
Secs is TotalSecs – HoursInSecs – MinsInSecs.
2. How many
a. Clauses? D.
b. Facts?
c. Rules?
d. Predicates?
e. Variable Terms?
f. Constant Terms?
A. B. C.
E. F.
Clauses: 1
Facts: 0
Rules: 1 Predicates: 1 Variable Terms: 6 Constant Terms: 0
Room#: 2
1. In prolog, data structures are represented as clauses.
For example, we can represent books using the structure book(Title, Author).
And authors can be represented by the structure author(FirstName, LastName).
a. Write a fact owns/2 that indicates that James owns a book named ‘Big Fish’ by an author named Emily Schue.
b. Write a fact that indicates that Julia owns a book by an author whose first name is Isaac.
c. Write a rule similar/2 that takes two names, and is true if the two named people own books by the same author.
owns(“James”, book(“Big Fish”, author(“Emily”, “Schue”))). owns(“Julia”, book(_, author(“Isaac”, _))).
owns(“Tom”, book(“Small Fish”, author(“Emily”, “Schue”))). similar(Name1, Name2) :-
owns(Name1, book(_, X)), owns(Name2, book(_, X)), Name1 \= Name2.
Room#: 3
1. In prolog, data structures are represented as clauses.
For example, we can represent books using the structure book(Title, Author).
And authors can be represented by the structure author(FirstName, LastName).
a. Write a fact owns/2 that indicates that James owns a book named ‘Big Fish’ by an author named Emily Schue.
b. Write a fact that indicates that Julia owns a book by an author whose first name is Isaac.
c. Write a rule similar/2 that takes two names, and is true if the two named people own books by the same author.
owns(‘James’, book(‘Big Fish’, author(‘Emily’, ‘Schue’))).
owns(‘Julia’, book(_, author(‘Isaac’, _))). similar(N1,N2) :-
N1 \= N2,
owns(N1, book(_, A)), owns(N2, book(_, A)).
1.
Room#: 1
In prolog, data structures are represented as clauses.
For example, we can represent books using the structure book(Title, Author).
And authors can be represented by the structure author(FirstName, LastName).
a. Write a fact owns/2 that indicates that James owns a book named ‘Big Fish’ by an author named Emily Schue.
b. Write a fact that indicates that Julia owns a book by an author whose first name is Isaac.
c. Write a rule similar/2 that takes two names, and is true if the two named people own books by the same author.
owns(‘James’, book(‘Big Fish’, author(‘Emily’, ‘Schue’))).
owns(‘Julia’, book( _, author(‘Issac’, _ ))).
similar(X, Y) :- owns(X , book( _ , author( F, L))) , owns(Y, book( _ , author(F, L))).
Room#:
1. Assume a structure point(X, Y) exists and write the following rules.
a. A point is above/2 another point if its Y coordinate is less
b. A point is below/2 if …. You get the idea!
c. Write a rule left/2
d. Write a rule right/2
2. Assume a structure line(P1, P2) exists, where P1 and P2 are points.
a. Write a rule horizontal/2 that is true of a line is horizontal. That is, both points have the same Y coordinate.
b. Ditto for vertical
c. Write a rule length/2 that accepts a line as its
first argument and computes the length of the line in the second argument.
So you don’t mess up the code, start by writing out the formula for the distance between two points.
+x axis
0 5 10 15 0
5
10
+y axis
Room#: We are number one
|
|
|
Room#: Uno
D and E. The non-terminal “b” doesn’t exist in the grammar, so it can’t be A, B, or C
-> a c -> a c -> a c c -> a c c d
->
-> c
-> c c
-> c c c
Room#: 1
->
| b
-> b |
-> a
| a b
Room#: 2
Syn:
Syn:
Sem:
Syn:
Sem:
Syn: -> A | B | C
Sem: .actual_type <- look-up(.string)
Room#:
In section 3.4.5, the rules are:
● Types can be int or real
● On the RHS, if both types are int then the type of the
expression is int, otherwise real.
● The type on the LHS must match the type on the RHS
● Attributes are expected_type and actual_type.
Room#:
Using the VM described in the chapter (statements on the left), write an operational semantics definition of…..
a. Java do-while loop
b. A for loop with this syntax: for Var in 1 .. N by Step do L done.
c. A Java if-then-else.
Room#: One, I guess
Using denotational semantics, describe the meaning Mb of a bin_num as shown in the grammar below.
Mb(‘0’) def= 0
Mb(‘1’) def= 1 Mb(
Room#:
Using denotational semantics, describe the meaning Moct of a oct_num as shown in the grammar below.