Basics Lab

The "basics lab" has you working through a sequence of basic exercises in Scheme. 

Writing Excellent Code

An important part of this laboratory is to give you a chance to write excellent code. That is code that is developed using a sound method, well commented, well tested, and properly styled. The exercises themselves are not meant to be terribly hard; that is intentional. However, it is challenging to slow down and practice the art and craft of programming. See the page on coding style to see what I am hoping you will practice in this lab. 

Pair Programming

We will make use of pair programming throughout this course. I have spent many hundreds of hours engaged in the practice of pair programming, and find it is far more productive and enjoyable than hacking alone. 

Although cheezy, this 10-minute video (also available in other formats) explains the basics of pair programming better than any number of words I might write. Please watch it before starting the lab.

If you want to read more about pair programming, you can start with the agile resources page at NCSU, the resources on Laurie Williams's home page, or you can dive straight into what the eXtreme Programmers have to say.

Unit Testing

I have written up documentation on how to use SchemeUnit for unit testing. Learning to use SchemeUnit will make testing your interpreters far, far easier. Lets see if you can mix unit testing in from day one.

For problems 1-10, be sure to provide tests that you feel thoroughly exercise the code you wrote.

Exercises

  1. Develop a function that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean theorem is c2 = a2 + b2.

    HINT
    : This is really just an algebra problem, that culminates in the use of the Pythagorean theorem. Don't make it too complex! 

  2. Develop a function that computes how long after their deparature two trains will meet. Assume that the trains travel between two points, along a single section of track, going in opposite directions. The function should consume the trains' speeds and the starting distance between the trains. Speed is distance/time. 

  3. Develop a function that when given an initial amount of money (called the principal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional deposits or withdrawals are made and and that a month is 1/12 of a year. Total interest is the product of the principal, the annual interest rate expressed as a decimal, and the number of years. 

  4. Develop a function that when given the length and width of a rectangular floor and the edge length of a square tile will compute the whole number of tiles needed to cover the floor completely. 

  5. Develop a function that computes the area of a regular polygon given the length of one side and the number of sides. If n is the number of sides and s is the length of a side, the area of a regular polygon is equal to 1/4 * n * s2 * 1/(tan PI/n). 

  6. The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.

  7. Develop what-kind. The function consumes the coefficients a,b, and c of a quadratic equation. It then determines whether the equation is degenerate and, if not, how many solutions the equation has. The function produces one of four symbols: ’degenerate, ’two, ’one, or ’none. An equation is degenerate if a = 0.

  8. Develop a function that, given a number of seconds since the start of the year, returns a string formatted with the number of weeks, days, hours, minutes, and seconds that have elapsed since then. You should look up format in the documentation.

  9. Develop a function that takes a symbol representing the name of a language, and returns the symbol for hello in that language. Propose, implement, and test a reasonable error case for inputs that you do not handle. 

    HINT: The purpose of this problem is to use a cond statement. For example, when the user passes your function the symbol 'German, your function might return 'GutenMorgen.

  10. CHALLENGE: Develop a function that consumes four arguments: two symbols and two numbers. The symbols are either 'inclusive or 'exclusive. The function should return a function that, when invoked, consumes a single number and returns a boolean. In short, write a function that builds the functions you just wrote. Re-implement your answers to #10 using your new "function-builder." (Only dedicate time to this problem if you get done with everything else, and you want to explore a bit.)

    UPDATE: This problem is not at all essential for understanding where we are going this semester, and I'll probably pull it from the problem set in the next run of the course. No fear if you do not touch the challenge problem, and kudos if you do wrestle with it!

  11. Develop a function for each of the following number ranges. The function should consume a number and return a boolean. ( [ is exclusive, ( is inclusive.)

    a. [5,10)
    b. (-2,22]
    c. (-inf, -1] [1, +inf)

  12. Evaluate each of these expressions, one at a time, in the DrScheme Interactions pane. Note the message reported for each syntax error in your lab book.

    (+ (10) 20)
    (10 + 20)
    (+ +)
    

  13. Evaluate each of these functions, one at a time, in the Definitions pane. Note the errors, and make the appropriate corrections.

    (define (f 1)
      (+ x 10))
    (define (g x)
      + x 10)
    (define h(x)
      (+ x 10))
    

  14. Evaluate each of the following expressions in the DrScheme Interactions pane. Each expression is syntactically correct, but contains a semantic error. Note the error messages that result, and be certain you understand why they came about.

    (+ 5 (/ 1 0))
    (sin 10 20)
    (somef 10)
    

  15. Develop two additional problems similar to problems #1–11. The problems might draw on your knowledge of sports, the sciences, humanities, or computing. Use strings, symbols, numbers... anything of the "atomic" data structures in the Scheme programming language. For each problem, write a problem statement like those above, and provide a solution in keeping with the Design Recipe, including good tests. Talk to me if you get ambitious, and get stuck in trying to implement your masterpiece.

Programming for Mastery

kung-fu-ramenThese exercises are for mastery of foundational skills.

If you and your partner both feel you could reproduce this work from scratch, on your own, then you have achieved some mastery of the material presented here. 

If you or your partner need more practice, see me; I can develop another set of practice problems. Or, perhaps go off and try working the problems again on your own. 

If you have written excellent code, understand it, and have the time, you might explore the language further by reading some of the resources I've linked to... perhaps by reading ahead in HtDP, or looking at TSPL or Fixnum Days

Note many of the exercises in this lab come here or here or here (PDF). (Just acknowledging my sources/places I stole from.)

Feedback

I had 4 goals in assigning this first laboratory.

  1. Provide you with an introduction to writing simple functions in Scheme.
  2. Give you practice using SchemeUnit to write tests.
  3. Provide enough repetition to gain some mastery, but not so much repetition that it was tedious/boring.
  4. The lab would take between 4 and 8 hours to complete.

If you would please rate this first laboratory based on these goals, and provide whatever feedback you feel is necessary to improve this lab in the future, that would be excellent.