Lab Quiz Solutions, Foundations of Computing, Week 9 -- Tues Nov 28 2000

Name:


  1. The expression (read) reads one Scheme expression from the terminal and returns that expression (it does not evaluate the expression). What does (read) return when you type each of the following inputs?

    1. x
      x


    2. (x)
      (x)


    3. x y z
      x
      Here x is a single expression.


    4. "x y z"
      "x y z"
      The entire quoted string is a single expression.


    5. (x y z)
      (x y z)


    6. (x) (y) (z)
      (x)
      Here (x) is a single expression.


    7. (x y
      z)

      (x y z)
      Scheme reads past the line break to read the entire expression through the closing parenthesis. The line break here is not part of the expression, it is merely a separator between list elements.


    8. "x y
      z"

      "x y
      z"

      Scheme reads past the line break to read the entire expression through the closing quote. The line break here is part of the expression, a quoted string.


    9. (x (y (z)))
      (x (y (z)))
      Scheme reads the entire nested expression, including all the parentheses.


    10. (x (y (z))))
      (x (y (z)))
      Scheme reads the entire nested expression, including all the parentheses. The extra parenthesis is not part of the expression so Scheme does not read it.




  2. Given these definitions:

    (define (read-three)
      (read) (read) (read))
    
    (define (read-xyz)
      (let ((x (read)))
        (let ((y (read)))
          (let ((z (read)))
            (list x y z)))))
    
    (define (read-list)
      (list (read) (read) (read)))
    
    1. What does (read-three) return when you type this input?
      x y z
      z
      Scheme only returns the value of the last expression in the body of the procedure. See page 347 in Simply Scheme.



    2. What does (read-xyz) return when you type this input?
      x y z
      (x y z)
      The nested lets force Scheme to evaluate the three reads in order. See page 361 in Simply Scheme.



    3. What might (read-list) return when you type this input? Give two different correct answers.
      x y z
      (x y z)
      If the Scheme implementation evaluates expressions left-to-right.
      (z y x)
      If the Scheme implementation evaluates expressions right-to-left. See page 362 in Simply Scheme.