CS 5010: Guided Practice 4.1 — Solution

The question was:

Consider the following data definition:

    ;; A ListOfStrings (LOS) is one of
    ;; -- empty
    ;; -- (cons String LOS)
  

Which of the following expressions has a value that is a LOS ?

Here are the answers:

  1. (cons "abc" empty)
    Answer: Yes.
    empty is a LOS and "abc" is a string, so (cons "abc" empty) is a LOS.
  2. (cons "bcd")
    Answer: No.
    This expression has no value, because cons is given a wrong number of arguments.
  3. (cons "cde" (cons "ef" empty))
    Answer: Yes.
    This is a LOS.
  4. (cons "3" (cons "4" empty))
    Answer: Yes.
    This too is a LOS.
  5. (cons 3 (cons 4 empty))
    Answer: No.
    3 and 4 are numbers, not strings.

For debugging: Click here to validate.