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.
(define (not-legal? c) (or (not-number? c) (<= c 0)) ) |
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 |
|
a |
(define (double wd) |
|
b |
(define (double wd) |
|
c |
(define (double wd) |
|
a | (double apple) => (two word wd s) |
b | (double apple) => ERROR "unbound variable: s" |
c | (double apple) => ERROR "bad function" |
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 _____ _____ ) ) )
There are several correct answers; some possibilities: |
(define (precedes-in-year? date1 date2) (< (day-span '(january 1) date1) (day-span '(january 1) date2))) |
(define (precedes-in-year? date1 date2) (< (day-span date1 date1) (day-span date1 date2))) |
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
A correct version that needs three comparisons: |
(define (precedes-in-year? date1 date2) (cond ((< (month-number (month-name date1)) (month-number (month-name date2)) ) #t) ((> (month-number (month-name date1)) (month-number (month-name date2)) ) #f) ;; dates must be in the same month ((< (date-in-month date1) (date-in-month date2)) #t) (else #f) ) ) |
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.
(if (equal? 'that (or 'this 'that '(the other))) (se (word) (quote (to say))) (se (quote (+ 2 2)) 'always 'makes (se (word (word) 'a) '5)))Answer: (+ 2 2 always makes a 5), because (or 'this 'that '(the other)) returns this. or returns the first true thing it finds, and all words are true, so or returns this. (equal? 'that 'this) is false, so we do the last line. (word) makes a word with no letters, which is "". (word "" 'a) is a, since word takes all of the letters from all of the words and puts those letters into a new word. "" has no letters, so the only letter in the new word is a. Finally, (quote (+ 2 2)) is the same as '(+ 2 2), which is a sentence. It does not become 4.
(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
(define (bunch-words sent) (cond ((= (count sent) 1) sent)Now what? Well, we want to see if the last letter of the first word is equal to the first word of the second letter. How do we get the last letter of the first word? (first sent) gives us the first word, so (last (first sent)) gives us the last letter of the first word. How about the first letter of the second word? Well, (bf sent) throws away the first word and (first (bf sent)) gets the word right after the first word, so (first (first (bf sent))) gets the first letter of the second word. This gets us to
(define (bunch-words sent) (cond ((= (count sent) 1) sent) ((equal? (last (first sent)) (first (first (bf sent)))) ???)What do we do once we find two words that should go together? Let's put them together and throw away the first letter of the second word. How do we throw away the first letter of the second word? (first (bf sent)) is the second word, so (bf (first (bf sent))) takes the butfirst of the second word.
(define (bunch-words sent) (cond ((= (count sent) 1) sent) ((equal? (last (first sent)) (first (first (bf sent)))) (se (word (first sent) (bf (first (bf sent)))) (bunch-words (bf sent))))Now if the words don't match, we want to leave the first word alone and look at the rest of the words.
(define (bunch-words sent) (cond ((= (count sent) 1) sent) ((equal? (last (first sent)) (first (first (bf sent)))) (se (word (first sent) (bf (first (bf sent)))) (bunch-words (bf sent)))) (else (se (first sent) (bunch-words (bf sent))))))Let's test it: (bunch-words '(this stuff freaks me out)).
(define (bunch-words sent) (cond ((= (count sent) 1) sent) ((equal? (last (first sent)) (first (first (bf sent)))) (bunch-words (se (word (first sent) (bf (first (bf sent)))) (bf (bf sent))))) (else (se (first sent) (bunch-words (bf sent))))))Yeah, that would work. You should test it, just to make sure.
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))Answer: Change year to read
(define (year date) (if (equal? (last date) 'ce) (item 3 date) (* -1 (item 3 date))))That's it. The month and day haven't changed.
Part B: Is it necessary to change anything else in the case study? Why or why not?
Answer: You don't need to change anything else. The instructions said that you and your partner had it working for negative numbers, and the new year returns negative numbers for BCE and positive numbers for CE. That's why we use year, month-name, and day-of-month.