Sample Problems for Midterm 1

fall 2004

Problem 1

Consider the procedure legal? below:

(define (legal? c)
(and (number? c) (> c 0)) )

Write a procedure not-legal? that returns true when legal? returns false, and vice versa. Your not-legal? procedure should be a combination of uses of the following procedures: and, or, >= (greater than or equal to), <= (less than or equal to), and not-number? (which returns true when number? returns false, and vice versa); it should not use cond, if, or not.

Problem 2

Consider a procedure named double that, given a word as argument, returns a two-word sentence. The first word is two. The second word is the result of adding an "s" to the end of the argument. Some examples of how double should behave appear below.

expression

intended result

(double 'apple)

(two apples)

(double 'bus)

(two buss)

(double 'box)

(two boxs)

Now consider some incorrect implementations of double. For each one, indicate what the call

(double 'apple)

will return. If no value is returned because the procedure crashes, give the error message that results.

procedure

returned value or error message

(define (double wd)
(sentence 'two '(word wd s)))


(define (double wd)
(sentence 'two (sentence wd s)) )


(define (double wd)
(sentence 'two (wd 's)) )


Problem 3

Fill in the blanks in the procedure below so that the code agrees with the comments.

; Given dates date1 and date2 in the form used in the Difference
; between Dates programs, return true when date1 is earlier in
; the year than date2, and return false otherwise.
; For example, (precedes-in-year? '(february 1) '(march 3)) should
; return true, while (precedes-in-year? '(february 1) '(january 17))
; and (precedes-in-year? '(february 1) '(february 1)) should return
; false.
(define (precedes-in-year? date1 date2)
(<
(day-span _____ _____ )
(day-span _____ _____ ) ) )

Problem 4

Using only calls to month-name, date-in-month, month-number, and numeric comparison procedures, complete the alternative version of precedes-in-year? below. Your solution should use as few comparisons as possible.

; Given dates date1 and date2 in the form used in the Difference
; between Dates programs, return true when date1 is earlier in
; the year than date2, and return false otherwise.
(define (precedes-in-year? date1 date2)
(cond

 

Problem 5

For the following Scheme expressions, write the value that the STk would output if you were to type it in. If an expression produces an error, then just write ERROR.

  1. (first '(things first))
  2. (first (bf (item 3 '(thom colin johnny ed phil)))
  3. (if (equal? 'that (or 'this 'that '(the other)))
          (se (word) (quote (to say)))
          (se (quote (+ 2 2)) 'always 'makes (se (word (word) 'a) '5)))
  4. (and (not '(hello)) (or (and (/ 5 0)) #t))
  5. (se ('tell 'me 'why))

Problem 6

For this function:
(define (mystery x)
  (or  (and (zero? x) '(+ x 5))
       (and (= x 3) 5)
       (se 'o (mystery (- x 2)))))
Give the results of the following calls. If a call results in an error, write ERROR. If it results in an infinite recursion, write INFLOOP
  1. (mystery 1)
  2. (mystery 2)
  3. (mystery 3)
  4. (mystery 8)
  5. (mystery 7)

Problem 7

Write a procedure called bunch-words, which takes in a sentence. If the last letter of one word is the same as the first letter of the following word, then the two words should be joined together with the common letter removed from one of them. See the examples:

Problem 8

You and your partner have just expanded your day-span procedure to work with any two dates in any years, even years before zero which are currently represented as negative numbers (the year -1 refers to 1 BCE). Your partner suggests that you switch to the more conventional representation with CE and BCE. For example, instead of (may 1 1984) you would use (may 1 1984 ce), and instead of (august 3 -50) you would use (august 3 50 bce).

Part A: How would you change the following accessors to accomodate this change?

(define (year date)
  (item 3 date))

(define (month-name date)
  (first date))

(define (day-of-month date)
    (item 2 date))

Part B: Is it necessary to change anything else in the case study? Why or why not?