Assignment 0: Finger Exercises -- Solutions



  1. Convert each of the following expressions into Scheme:

    1. 4 + 6 + 7

      Solution: (+ 4 6 7) or (+ 4 (+ 6 7))

    2. 5 * (5 + 9)

      Solution: (* 5 (+ 5 9))

    3. (4 + 9) * (8 - 7)

      Solution: (* (+ 4 9) (- 8 7))

    4. ((a * a) + (b * b)) / 2

      Solution: (/ (+ (* a a) (* b b)) 2)

  2. Convert the following function definitions into Scheme:

    1. square(x) = x * x

      Solution: (define (square x) (* x x))

    2. avg(x, y) = (x + y) / 2

      Solution: (define (avg x y) (/ (+ x y) 2))

    3. celc->fahr(deg) = 9/5 * deg + 32

      Solution: (define (celc->fahr deg) (+ (* (/ 9 5) deg) 32))

  3. Evaluate each of the following expressions. Show every step. For example:
      (sqrt (+ (* 3 3) (* 4 4)))
    = (sqrt (+ 9 (* 4 4)))
    = (sqrt (+ 9 16))
    = (sqrt 25)
    = 5
    

    1. (- (* 3 5) 20)

      Solution:

       
        (- (* 3 5) 20)
      = (- 15 20)
      = -5
      

    2. (/ 44 (+ 8 3))

      Solution:

        (/ 44 (+ 8 3))
      = (/ 44 11)
      = 4
      

    3. (sqrt (square 6))

      Solution:

        (sqrt (square 6))
      = (sqrt (* 6 6))
      = (sqrt 36)
      = 6
      

    4. (avg 6 10)

      Solution:

        (avg 6 10)
      = (/ (+ 6 10) 2)
      = (/ 16 2)
      = 8
      

    5. (celc->fahr 100)
        (celc->fahr 100)
      = (+ (* (/ 9 5) 100) 32))
      = (+ (* 9/5 100) 32)
      = (+ 180 32)
      = 212
      





Kathi Fisler This page was generated on Fri Aug 27 17:01:35 CDT 1999.