CS 331 Spring 2025 > Exam Review Problems, Set F
CS 331 Spring 2025
Exam Review Problems, Set F
This is the sixth set of exam review problems.
Problems
Review problems are given below. Answers are in the Answers section of this document. Do not turn these in.
Answers
- Prolog has dynamic typing.
- Prolog’s typing is implicit.
- Prolog has a fixed set of types.
- Prolog types do not apply to variables; only values have types.
- False. A Prolog variable must begin with an upper-case letter or an underscore.
- True.
xyz
and'xyz'
name the same Prolog atom.
- To simulate a function using a Prolog predicate, add an additional argument to be the function’s output. For example, to simulate a function that takes one argument and returns its square, we use a 2-argument predicate whose second argument is the square of its first argument, with the second argument allowing output.
- In Prolog documentation, abc/2 means that abc is a predicate with 2 parameters.
- In Prolog documentation, xyz(+a, –b, ?c) means that xyz is a predicate with 3 parameters. The first, a, is input-only; it cannot be a free variable. The second, b, is output-only; it must be a free variable. The third, c, is input or output.
- In Prolog,
=
means can-be-unified.is
evaluates the numerical expression that is its second (right-hand) argument and attempts to unify the result with its first (left-hand) argument. - In Haskell, the first definition that matches is used,
with any later definitions being ignored.
In Prolog,
all matching definitions are used,
unless this is prevented with a cut (
!
). - A Prolog singleton variable is a variable that appears in a pattern but is never otherwise used.
- We can prevent singleton-variable errors
by replacing the variable in the pattern
with an underscore (
_
).
- The primary Prolog syntax for lists
has a comma-separate list of items enclosed in brackets,
for example,
“
[1, 2, 3]
”. - The syntax for tuples is much the same as that for lists,
except that parentheses are used, if necessary, instead of brackets:
“
(1, 2, 3)
”. - True.
A Prolog list may contain items of different types.
For example, “
[1, 'abc']
”
- The primary Prolog syntax for lists
has a comma-separate list of items enclosed in brackets,
for example,
“
[1 | 2]
is the corresponding Prolog pair.- We write a Prolog cut as an exclamation point
(
!
). - Cut is a zero-argument predicate. It always succeeds, and then prevents backtracking past the cut, for the current goal. This prevention includes forbidding use of later definitions of a predicate for the current goal.
- We write a Prolog cut as an exclamation point
(
- In Prolog,
true
andrepeat
are both zero-argument predicates that always succeed. Buttrue
succeeds once, whilerepeat
succeeeds an unlimited number of times. repeat
can be used as the beginning of (roughly) a “while (true)
” loop.
- In Prolog,
- Functionality is nondeterministic if it can give different results for the same input.
- Commonly used nondeterministic functionality includes pseudorandom number generation and user console input.
- In Prolog, a semicolon (
;
) is another way to separate terms in the body (right-hand side) of a rule. It means OR, where a comma (,
) means AND. - Using a semicolon can replace the use of multiple rules for a single predicate.
- In Prolog, a semicolon (
- All four of the programming languages we studied have typing that is primarily implicit.
- Haskell allows for the definition of new types. Lua, Scheme, and Prolog do not.
- All four of the programming languages we studied allow for recursion.
- Lua, Haskell, and Scheme have a Boolean type that is separate from all numeric types, and allow variables to hold these Boolean values. Prolog does not.