CS 307: 1. Basic Scheme

Due: Wednesday, Sept 5, 2001.

  1. Start DrScheme. Put the following commands in the Definitions window and execute them using the green arrow (see drscheme.html):
    (current-directory "c:/program files/plt/")
    (load "initdr.scm")
  2. Evaluate the following using Scheme:
    (/ 17 7)          (+ (* 3 5) (/ 14 2))         (- 14 3)
    (/ 17 -7)         (truncate (/ 17 7))          (/ 17 7.0)
    (modulo 17 7)     (sin 0.75)                   (atan 3 4)
    (sqrt -5)         (if (> 3 4) (+ 3 4) (* 3 4))
    Turn in a printout of your interactions showing what you typed in to the Scheme Interpreter and the results you received. This assignment is easy; it requires you to start using Scheme and to learn how to print out your assignments. Name the four different kinds of numbers that you observe in the results of the above expressions.
  3. Define a function f(x) = x*x - 5*x + 6 and evaluate it for x = 0, 1, 2, 3, 4 .
  4. Write a function (quadratic-root a b c) that finds one root of a quadratic equation a*x*x + b*x + c = 0 using the quadratic formula. Use it to find a root of the above equation. What are (quadratic-root 1 0 1) and (quadratic-root 1 0 2)? Would these two examples work in other programming languages that you know?
  5. Write a function (poly-area r n) to compute the area of a regular n-sided polygon inscribed in a circle of radius r. Demonstrate your function on several data values. What does (poly-area 1.0 n) approach as n becomes large? (Try it! Use several values of n, such as 100, 100000, 100000000.) (Note: Include answers to questions on the homework you turn in.)
  6. See the comments in the file initdr.scm to find out how to use the drawing functions. Write a function (draw-rectangle x y width height) that will draw a rectangle whose lower-left corner is (x y) and whose width and height are as specified. Make a drawing that contains several rectangles of different sizes, drawn at different places.
  7. Write a function (print-in-box s x y) that will print a string s in the window inside a rectangle whose lower-left corner is (x y), calling draw-rectangle. Use the length of the string to calculate the approximate size of the rectangle that is needed. The function (string-length s) returns the length of a string in characters. A string constant is written inside double quotes: "Bevo" . Use print-in-box to print your name in the window in a box. The characters of the string should not touch or be on top of the lines of the box:

    Philo T. Farnsworth

  8. Write a function (print-fancy s x y) that will print a string s in the window inside a rectangle whose lower-left corner is (x y). Add some decoration to the rectangle to make it look fancier.
  9. Download the Scheme Tutor files from ftp://ftp.cs.utexas.edu/pub/novak/cs307/; put them into the Plt directory. Double-click the file drtutor.scm and then click the green Execute button. Run the math-functions module and hand in your results.