INTRODUCTION TO CS *UC IRVINE * D. G. KAY   SCHEME FINGER EXERCISES * PAGE

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.

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 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))))))))


  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.  


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


    1. Include some conds.


  9. Symbols.


  10. What type.


  11. Structure definitions


  12. Structure manipulations--constructors


  13. Structure manipulations--field selectors


  14. Structure manipulations--type-checking predicates


  15. What type


  16. Function definitions


    1. lambda; 1 arg, 2 args, 3 args


  17. Function calls


  18. What type returned?


  19. List stuff


  20.  


  21.  


  22.  


  23.  


  24.  


  25.  


  26.  


  27.  


  28.  


  29.  


  30.  




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 -- 8:48 AM