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.

  1. Describe Prolog’s type system.
    1. Static or dynamic?
    2. Mostly manifest or mostly implicit?
    3. Fixed set of types or extensible?
    4. Types apply to variables?
    1. True or false: A Prolog variable must begin with a lower-case letter.
    2. True or false: xyz and 'xyz' name the same Prolog atom.
  2. How can we simulate a function using a Prolog predicate?
    1. In Prolog documentation, what does abc/2 mean?
    2. In Prolog documentation, what does xyz(+a, –b, ?c) mean?
  3. Explain = and is in Prolog.
  4. Both Haskell and Prolog allow for multiple definitions—of functions and predicates, respectively—that involve pattern matching. However, how the existence of multiple definitions is handled is fundamentally different between the two programming languages. Explain.
    1. What is a Prolog singleton variable?
    2. How can we prevent singleton-variable errors?
    1. Describe the primary Prolog syntax for lists.
    2. Describe the Prolog syntax for tuples.
    3. True or false: A Prolog list may contain items of different types.
  5. (1 . 2) is a Scheme pair. How do we write a pair holding the same values in Prolog?
    1. How do we write a Prolog cut?
    2. What does a Prolog cut do?
    1. Explain true vs. repeat in Prolog.
    2. The behavior of repeat makes it useful in emulating a common flow-of-control construct. Explain.
    1. What do we mean when we say a function or predicate is nondeterministic?
    2. Give examples of commonly used nondeterministic functionality.
    1. What does a semicolon (;) do in Prolog?
    2. Fill in the blank. Using a semicolon can replace the use of multiple ________ for a single predicate.
  6. Of the four programming languages we studied this semester …
    1. … which have typing that is primarily implicit?
    2. … which allow for the definition of new types?
    3. … which allow for recursion?
    4. … which have a Boolean type that is separate from all numeric types, and allow variables to hold these Boolean values?

Answers

    1. Prolog has dynamic typing.
    2. Prolog’s typing is implicit.
    3. Prolog has a fixed set of types.
    4. Prolog types do not apply to variables; only values have types.
    1. False. A Prolog variable must begin with an upper-case letter or an underscore.
    2. True. xyz and 'xyz' name the same Prolog atom.
  1. 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.
    1. In Prolog documentation, abc/2 means that abc is a predicate with 2 parameters.
    2. 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.
  2. 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.
  3. 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 (!).
    1. A Prolog singleton variable is a variable that appears in a pattern but is never otherwise used.
    2. We can prevent singleton-variable errors by replacing the variable in the pattern with an underscore (_).
    1. The primary Prolog syntax for lists has a comma-separate list of items enclosed in brackets, for example, “[1, 2, 3]”.
    2. 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)”.
    3. True. A Prolog list may contain items of different types. For example, “[1, 'abc']
  4. [1 | 2] is the corresponding Prolog pair.
    1. We write a Prolog cut as an exclamation point (!).
    2. 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.
    1. In Prolog, true and repeat are both zero-argument predicates that always succeed. But true succeeds once, while repeat succeeeds an unlimited number of times.
    2. repeat can be used as the beginning of (roughly) a “while (true)” loop.
    1. Functionality is nondeterministic if it can give different results for the same input.
    2. Commonly used nondeterministic functionality includes pseudorandom number generation and user console input.
    1. 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.
    2. Using a semicolon can replace the use of multiple rules for a single predicate.
    1. All four of the programming languages we studied have typing that is primarily implicit.
    2. Haskell allows for the definition of new types. Lua, Scheme, and Prolog do not.
    3. All four of the programming languages we studied allow for recursion.
    4. 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.