Introduction to Computer Science * UC Irvine * David G. Kay
SCHEME FINGER EXERCISES
We provide these short exercises to give you practice with some of the fundamental mechanics of Scheme. We recommend that you try exercises in each section. The later exercises in each section are more complex than the earlier ones, so if you feel comfortable with the topics, just try the last exercise or two in a section. If you find that you can't do them quickly and correctly, try the earlier ones first Of course you should ask your TA or instructor if you'd like further explanation. [Since these are brand new, it is just barely possible that some errors have crept in. If you think you've found one, please contact kay@uci.edu.]

The answers appear on this page in white text; you can view them by selecting or highlighting the area below the problem. You'll get the greatest benefit by using pencil and paper to write down your own answer before looking at ours; otherwise, it's too easy just to browse through, thinking, "Yes, that looks right," without building your own essential understanding. (And surely this is obvious, but you won't get any benefit at all out of just copying and pasting these problems into DrScheme for evaluation. The answers are here already, and DrScheme already knows how to evaluate these expressions; the point is for you to work them out by hand and build your own skills.)

If we don't show the definition of a function here, that function is predefined in (built into) DrScheme; the Help Desk will describe it.

  1. Simple and nested arithmetic expressions. What is the value of each expression?

    1. (+ 25 30)
      55

    2. (+ 40 50 60)
      150

    3. (- 15 5)
      10

    4. (- 5 15)
      -10

    5. (* 5 (+ 2 3))
      25

    6. (/ 100 (- 7 2))
      20

    7. (* (- 10 2) (+ 1 4))
      40

    8. (+ (- 12 4) (* 2 (+ 5 1)))
      20

    9. (+ (* (- 5 3) 6) 3)
      15

    10. (+ (* 5 6) (/ (* 6 4) (- 20 (* 7 2))) (- 5 10))
      29

  2. Defining constants. What is the value returned at the end of each sequence of expressions?

    1. (define SIZE 15)
      (+ 1 SIZE)
      16

    2. (define HEIGHT 10)
      (define WIDTH 20)
      (* WIDTH HEIGHT)
      200

  3. Defining functions. What is the value returned at the end of each sequence of expressions?

    1. (define triple
         (lambda (num)
           (* 3 num)))
      (triple 5)
      15

    2. (define area
         (lambda (height width)
           (* height width)))
      (area 10 12)
      120

    3. (define total-pennies
         (lambda (dollars cents)
           (+ (* dollars 100) cents)))
      (total-pennies 5 43)
      543

    4. ;; [Same definition of total-pennies]
      (total-pennies 43 5)
      4305

    5. (define total-restaurant-bill
         (lambda (base-amount taxrate tiprate)
           (+ base-amount (* taxrate base-amount) (* tiprate base-amount))))
      (total-restaurant-bill 100.00 0.05 0.20)
      125.00

    6. (define three
         (lambda (huey dewey louie)
           (+ dewey huey (* dewey louie))))
      (three 2 3 4)
      17

    7. ;; [Same definition of three]
      (three 4 3 2)
      13

    8. (define SECONDS-PER-MINUTE 60)
      (define MINUTES-PER-HOUR 60)
      (define HOURS-PER-DAY 24)
      (define total-seconds
         (lambda (days hours minutes seconds)
           (+   seconds
             (*   SECONDS-PER-MINUTE
               (+   minutes
                 (*   MINUTES-PER-HOUR
                   (+ hours (* HOURS-PER-DAY days))))))))
      (total-seconds 0 0 1 3)
      63

    9. ;; [Same definitions of total-seconds and related constants]
      (total-seconds 0 2 0 7)
      7207

    10. ;; [Same definitions of total-seconds and related constants]
      (total-seconds 1 1 1 1)
      90061

  4. Boolean expressions with relational operators. What is the value of each expression?

    1. (= 12 (* 4 3))
      true

    2. (> 12 (* 4 4))
      false

    3. (<= (* 5 4) (* 3 7))
      true

    4. (< (* 6 3) (* 9 2))
      false

  5. Boolean expressions with predicate functions. What is the value of each expression? As you evaluate each sub-expression, ask yourself what type of value (number, boolean, ...) the function returns.

    1. (even? 16) ; Is the input an even number?
      true

    2. (odd? (* 6 3))
      false

    3. (number? (= 5 (+ 3 2))) ; Is the input a number?
      false; = returns a boolean, not a number

    4. (equal? (negative? (- 10 5)) (odd? (* 4 2)))
      true; both inputs to equal? are false

  6. Boolean expressions with logical operators. What is the value of each expression?

    1. (and (= 5 (+ 4 1)) (even? 12))
      true

    2. (and (odd? 17) (odd? 18) (odd? 19))
      false

    3. (and (even? 100) (odd? 101) (even? 102) (odd? 103))
      true

    4. (and (odd? 2) (odd? 4) (odd? 6))
      false

    5. (or (= 5 (+ 4 1)) (even? 12))
      true

    6. (or (odd? 17) (odd? 18) (odd? 19))
      true

    7. (or (even? 100) (odd? 101) (even? 102) (odd? 103))
      true

    8. (or (odd?2) (odd? 4) (odd? 6))
      false

    9. (not (odd? 13))
      false

    10. (not (even? 13))
      true

    11. (not (= (+ 4 3) 7))
      false

    12. (not (= (* 4 2) 9))
      true

    13. (or (odd? 14) (and (= (+ 6 1) 7) (< (+ 6 2) 9)))
      true

    14. (and (or (= (+ 6 1) 7) (< (+ 6 2) 9))
           (odd? 14))
      false

    15. (define A true)
      (define B false)
      (equal? (not (and A B))
              (or (not A) (not B)))
      true; in fact, this is one of DeMorgan's laws. It's true no matter what A and B are.

    16. (define A false)
      (define B false)
      (equal? (not (or A B))
              (and (not A) (not B)))
      true; this is another of DeMorgan's laws.

  7. Cond expressions. What is the value returned by each expression?

    1. (cond
         ((= 6 (- 5 1)) 17)
         (else 18))
      18

    2. (cond
         ((even? (* 5 4)) 21)
         (else 20))
      21

    3. (cond
         ((> (sqrt 25) 5) (* 3 5))
         ((< (sqrt 25) 5) (* 4 5))
         ((= (sqrt 25) 5) (* 5 5)))
      25

    4. (define choose
         (lambda (num)
           (cond
             ((not (number? num)) 15)
             ((odd? num) 16)
             ((positive? num) 17)
             (else 18))))
      (choose 16)
      17

  8. Symbols. What is the value returned by each expression?

    1. (cond
         ((odd? (* 5 2)) 'pancake)
         ((negative? (- 2 5)) 'flapjack)
         (else 'griddlecake))
      flapjack

    2. (define classify
         (lambda (item)
           (cond
             ((symbol=? item 'animal) 25)
             ((symbol=? item 'vegetable) 30)
             ((symbol=? item 'mineral) 35)
             (else 'none-of-these))))
      (classify 'vegetable)
      30

    3. ;; [Same definition of classify]
      (classify 'love)
      none-of-these; in some circumstances, the Scheme interpreter will display this with a quotation mark in front---quotes matter when you're writing code, but not so much when displaying results.

    4. ;; [Same definition of classify]
      (classify 'dog)
      none-of-these; symbols are just (non-numeric) data---they have no intrinsic meaning

  9. Strings. We use strings for textual data--characters we can type on a keyboard--that may include spaces and other characters that can't be in symbols. Many other operations on strings are described in the Help Desk. What is the value returned by each expression?

    1. "Hello there!"
      "Hello there!"; the value of a constant, double-quoted string is the string itself. There are ways to display strings without the quotation marks, of course.

    2. (string-append "See you" "later")
      "See youlater"

    3. (string=? "See you later" (string-append "See you" " " "later"))
      true

  10. Expression types: What type of value (number, boolean, symbol, string, function) does each expression return?

    1. (+ 7 5)
      number; we're asking for the type, not the actual value

    2. (* 5 (sqrt (/ 134234 17))
      number

    3. (= (* 6 4) (+ 20 (- 10 6)))
      boolean; did you notice that you don't have to do the calculation?

    4. (equal? (* (* (* 5 7) 8) 9) (/ 100000 (/ 1000 10)))
      boolean

    5. (number? (* 5 (+ 3 4)))
      boolean

    6. (cond
         ((= 5 (sqrt 25)) "Exact match.")
         ((< 12 (* 6 4)) "Below the line.")
         (else "Hard to say."))
      string

    7. (cond
         ((odd? (* 5 2)) 'thyme)
         ((negative? (- 2 5)) 'sage)
         (else 'chervil))
      symbol

    8. (define age-can-do
         (lambda (age)
           ((< age 18) 'neither)
           ((and (>= age 18) (< age 21)) 'vote-not-drink)
           (else 'both)))
      (age-can-do 20)
      symbol

    9. (lambda (x) (* 2 x)) ; slightly advanced, but we have discussed this
      function; lambda expressions return functions [also called procedures]---they describe a machine with input(s) and an output (return value). In this case, the machine described by the lambda expression apparently returns a number, but the lambda expression itself returns a machine/function/procedure.

  11. Structure definitions. Write the Scheme code to define a structure that represents the data described below.

    1. a piece of carpet, with a length and a width (both numbers, representing feet)
      (define-struct carpet (length width)); the code doesn't specify what types the fields are, but your data definition (in a comment) would.

    2. a shipping box, with a length, width, depth, weight, and material (all numbers--lengths in inches, weight in ounces--except the last, which is a symbol chosen from 'cardboard, 'wood, or 'plastic)
      (define-struct shipping-box (length width depth weight material))

    3. a student, with a name (string), ID number, and major (symbol).
      (define-struct student (name ID major))

  12. Structure manipulations--constructors. Use the structure definitions in the previous part to create and return each of the objects (also called instances) described below.

    1. a 5-by-7 piece of carpet
      (make-carpet 5 7)

    2. a very large piece of carpet that's 8998789 feet wide and twice as long (do this with copy and paste and without a calculator)
      (make-carpet (* 2 8998789) 8998789)

    3. a wooden shipping box that's 4-by-5-by-10 and weighs 36 ounces
      (make-shipping-box 4 5 10 36 'wood); make sure you keep the order of the fields straight when you create the object, even if the English description gives them in some other order.

    4. A 17-ounce cubical plastic box that's 18 inches on a side.
      (make-shipping-box 18 18 18 17 'plastic)

    5. An English major named Peter Anteater whose ID is 11223344.
      (make-student "Peter Anteater" 11223344 'English); as we move towards real-world problems (and away from finger exercises), we may need to make real-world decisions like whether to store the name last-name-first and just what the list of recognized symbols for majors will be.

  13. Structure manipulations--field selectors. Using the same structure definitions, what value does each sequence of expressions below return?

    1. (define my-box (make-shipping-box 20 30 40 15 'cardboard))
      (shipping-box-length my-box)
      20

    2. ;; [Same definition of my-box]
      (shipping-box-weight my-box)
      15

    3. ;; [Same definition of my-box]
      (* (shipping-box-length my-box) (shipping-box-width my-box))
      600

    4. (make-student (string-append "Annabelle" " " "Anteater") 55443322 'ChemEngr)
      a student structure with name "Annabelle Anteater", ID 55443322, major ChemEngr. DrScheme represents this simply as (make-student "Annabelle Anteater" 55443322 'ChemEngr)

  14. Structure manipulations--field selectors. Using the same structure definitions, write the Scheme functions described below.

    1. ;; shipping-box-footprint: shipping-box -> number
      ;; Return area shipping box occupies, e.g., on a shelf, irrespective of height
      (define shipping-box-footprint
         (lambda (SB)
           (* (shipping-box-length SB) (shipping-box-width SB))))

    2. ;; shipping-box-volume: shipping-box -> number
      ;; Return volume of shipping box (length times width times height) in cubic inches
      (define shipping-box-volume
         (lambda (SB)
           (* (shipping-box-length SB) (shipping-box-width SB) (shipping-box-height SB))))
      ; or, perhaps better: (* (shipping-box-area SB) (shipping-box-height SB)) ))

    3. ;; fits-on-carpet?: shipping-box carpet -> boolean
      ;; Return true if the carpet's area is at least the area of the shipping box.
      ;; Use the definitions above if they're helpful.
      (define fits-on-carpet?
         (lambda (SB C)
           (<= (shipping-box-footprint SB)
             (* (* 12 (carpet-length C)) (* 12 (carpet-width C))))))
      ;; Don't forget that carpet widths are stored in feet, not inches.

  15. What type of value (number, boolean, symbol, string, carpet, shipping-box, student, function) does each expression return? Use the definitions above where appropriate.

    1. (make-shipping-box 10 10 5 6 'plastic)
      shipping-box

    2. (student-major (make-student "Petra Programmer" 11221122 'CompSci))
      symbol

    3. (make-student "Alan Turing" 33445566 'Math)
      student

    4. (symbol=? 'Informatics (student-major (make-student "Petra Programmer" 11221122 'CompSci)))
      boolean

    5. (string-append (student-name (make-student "John Smith" 33444433 'History)) ", Ph.D.")
      string

    6. (define big-box (make-shipping-box 48 48 48 72 'wood))
      (define small-box (make-shipping-box 12 12 12 12 'wood))
      (+ (shipping-box-weight big-box) (shipping-box-weight small-box))
      number

    7. ;; [Same definitions as above]
      (> (shipping-box-volume big-box) (shipping-box-volume small-box))
      boolean

    8. (make-shipping-box
         (shipping-box-length big-box)
         (shipping-box-width big-box)
         (shipping-box-height big-box)
         (+ 12 (shipping-box-weight big-box))
         'hardwood)
      shipping-box



David G. Kay, 406B Computer Science
University of California, Irvine
Irvine, CA 92697-3425 -- (949) 824-5072 -- Fax (949) 824-4056 -- Email kay@uci.edu

Sunday, October 9, 2005 -- 2:27 PM