Final Lab Quiz Solutions, Foundations of Computing, Fall 2000

  1. The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.

    1. (+ 1 3)
      4

    2. (* 3 (+ 1 3))
      12



  2. For all parts in this questions, assume the following definitions have been executed:

    (define s '(a b c d))
    
    (define t '((a b) c d))
    

    The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.

    1. s
      (a b c d)

    2. (car s)
      a

    3. (cdr s)
      (b c d)

    4. (cons 'a '(b c d))
      (a b c d)

    5. (cons (car s) (cdr s))
      (a b c d)

    6. (car (cdr s))
      b

    7. t
      ((a b) c d)

    8. (car t)
      (a b)

    9. (cdr t)
      (c d)

    10. (cons '(a b) '(c d))
      ((a b) c d)

    11. (list '(a b) '(c d))
      ((a b) (c d))

    12. (append '(a b) '(c d))
      (a b c d)



  3. For all parts in this question, assume that a procedure named add-1 has been defined. It takes a number as an argument, and returns the number that is one greater than the argument. For example (add-1 3) evaluates to 4.

    The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.

    1. (map add-1 '(1 2 3))
      (2 3 4)

    2. (map (lambda (x) (* 2 x)) '(1 2 3))
      (2 4 6)



  4. Define a Scheme variable named x that has the value 3.

    (define x 3)
    


  5. This table shows the number of electoral votes allocated to several states:

    Washington11
    Oregon7
    Iowa7
    California54
    Florida25

    Define a Scheme variable named electoral-votes to encode this table. Represent the table by an association list: a list of sublists, one for each state, where each sublist has two elements. The first element of each sublist is the name of the state and the second element is the number of electoral votes for that state.

    (define electoral-votes
      '((Washington 11) (Oregon 7) (Iowa 7) (California 54) (Florida 25)))
    


  6. Define a Scheme procedure named add-1 that takes a number as an argument, and returns the number that is one greater than the argument.

    (define (add-1 x)
      (+ 1 x))
    


  7. Define a Scheme procedure named phase that takes a number as an argument. If the number is less than zero, it returns the symbol ice. If the number is greater than one hundred, it returns the symbol steam. Otherwise it returns the symbol water.

    (define (phase x)
      (cond ((< x 0) 'ice)
            ((> x 100) 'steam)
            (else 'water)))
    


  8. This definition is executed:

    ;; Like Scheme length
    (define (len lst)
      (if (null? lst)
          0
          (+ 1 (len (cdr lst)))))
    

    Then this expression is typed into the Scheme interpreter and evaluated. It returns the result shown:

    > (len '(x y z))
    3
    

    The following questions all concern what happens when the expression (len '(x y z)) is evaluated.

    1. What is the base case?
      The empty list.

    2. What does len return when the base case is reached?
      0

    3. How many times is len called?
      4

    4. What is lst bound to when len is called the first time?
      (x y z)

    5. What is the value of (cdr lst) when len is called the first time?
      (y z)

    6. What is the value of (len (cdr lst)) when len is called the first time?
      2

    7. What is the value of (+ 1 (len (cdr lst))) when len is called the first time?
      3