Assignment 2: Starting Scheme


Due: 10:30 pm, Tuesday 28 January 2014

Summary: We consider some initial problems that you will solve with the Scheme programming language.

Purposes: To get you used to writing Scheme. To get you started thinking about algorithm design. To encourage you to do work outside of class.

Collaboration: You must work with assigned partners on this assignment. The partner assignments are available at http://www.cs.grinnell.edu/~weinman/courses/CSC151/2014S/partners.html. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Wrapper (Prologue): Individually read through this assignment and make sure that you understand what is required. Then use the form available at https://docs.google.com/forms/d/1NLdiGKtZu8Nt74Mg4QvRvoTAEgMP0UJ0m1C6bg2geYs/viewform to indicate (a) how long you think this assignment will take and (b) what you think will be the most challenging aspect of this assignment.

Wrapper (Epilogue): When you are done with the assignment, fill out the form available at https://docs.google.com/forms/d/1-GlUDHE1M89_F7lBXfT60BAAS8bJm7yuU3NjDrQQFfk/viewform to indicate (a) how long the assignment took, (b) what the most challenging part of the assignment was, and (c) something important you learned from doing the assignment. If you find that the assignment took much less or much more time than you expected, also include (d) a note as to what might have led to that difference.

Submitting the main assignment: Email your answer to . The title of your email should have the form CSC-151-01 Assignment 2 and should contain your answers to all parts of the assignment. Scheme code should be in the body of the message.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

Assignment

Part A: Rounding

You may recall that we have a number of mechanisms for rounding real numbers to integers, such as ceiling and floor. But what if we want to round not to an integer, but to only two digits after the decimal point? Scheme does not include a built-in operation for doing that kind of rounding. Nonetheless, it is fairly straightforward.

1. Suppose we have a value, val. Write instructions that give val rounded to the nearest hundredth. For example,

> (define val 22.71256)
> (your-instructions val)
22.71
> (define val 10.7561)
> (your-instructions val)
10.76

Now, let's generalize your instructions to round to an arbitrary number of digits after the decimal point.

Suppose precision is a non-negative integer and val is a real value. Write instructions for rounding val to use only precision digits after the decimal point.

> (your-instructions ... val ... precision ...)

As you write your instructions, you may find the expt useful. (expt b p) computes bp.

Part B: Quadratic Roots

One advantage of learning a programming language is that you can automate tedious computations. We consider such a computation here.

One of the more painful computations students are often asked to do in high-school mathematics courses is to compute the roots of a polynomial. As you may recall, a root is a value for which the value of the polynomial is 0. For example, the roots of 3x2 - 5x + 2 are 2/3 and 1.

Hmmm ... let's check that claim. 3*(2/3)*(2/3) - 5*2/3 + 2 = 12/9 + 10/3 + 2 = 4/3 - 10/3 + 2 = (4-10)/3 + 2 = -6/3 + 2 = -2 + 2 = 0. So far so good.

3*1*1 - 5*1 + 2 = 3 - 5 + 2 = -2 + 2 - 0. Yup, 2/3 and 1 are the roots.

There is, of course, a famous formula for computing the roots of a quadratic polynomial of the form ax2 + bx + c. In a narrative style, this formula is often expressed as

Negative b plus or minus the square root of b squared minus four ac all over two a.

In more traditional mathematical notation, we might write

(-b +/- sqrt(b2 - 4ac))/2a

Our goal, of course, is to convert all of these ideas to a Scheme program.

We can express the coefficients of a particular polynomial by using define expressions.

(define a 3)
(define b -5)
(define c 1)

If we also define x, we can evaluate the polynomial.

(define x 5)
(define value-of-polynomial (+ (* a x x) (* b x) c))

Of course, since we've defined a, b, and c, we can also compute the roots. Here is a bit of incorrect code to compute roots.

(define root1 (/ (+ (- b) (sqrt b)) 
                 (* 2 a)))
(define root2 (/ (- (- b) (sqrt b)) 
                 (* 2 a)))

Your goal, of course, is to correct the code for root1 and root2 so that we correctly compute the roots of the polynomial.

You can test the correctness of your solution by trying something like

(define value-of-polynomial-at-root1 (+ (* a root1 root1) (* b root1) c))
(define value-of-polynomial-at-root2 (+ (* a root2 root2) (* b root2) c))

Note: In the past, we've seen students test their quadratic-root formula by trying essentially arbitrary values for a, b, and c. Unfortunately, many arbitrary quadratic polynomials have no real roots. Hence, we suggest that you test your work by building polynomials for which you know the roots. How? Create your polynomials by multiplying two linear polynomials, (px + q) * (rx+s). You know that the roots of this polynomial will be -q/p and -s/r.

Part C: Learning About Numeric Functions

Scheme provides a number of numeric procedures that can produce integer results. We have already explored expt, abs, +, -, *, and /.

Here are some others. For each, try to learn (by experimentation, by discussing results with other students, and, eventually, by reading documentation) how many parameters each procedure can take and what the procedure does. Make sure to try a variety of input values for each procedure, including positive and negative, integer, real, and complex.

  • quotient
  • remainder
  • modulo
  • max
  • min
  • numerator
  • denominator
  • gcd
  • lcm

After you've figured out each procedure, write a short explanation of what the procedure does, one suitable for your peers in the class who may not have encountered the procedure before. In writing your explanation, you should include illustative samples of the use of each procedure. You may also find it helpful to include examples of failure. For example,

The abs function computes the absolute value of a number. If given a positive number as input, it returns the same number. If given a negative number as input, it returns the oppositve of the number.

> (abs 5)
5
> (abs -2.3)
2.3
> (abs 1/3)
1/3
> (abs -2/5)
2/5

The return value is the same type as the input. If abs is given an integer as input, it returns an integer. If abs is given an exact input, it returns an exact output. If abs is given an inexact input, it returns an inexact output.

The abs function expects an integer or real number as an input. If given a complex number or non-number as input, it reports an error. If given zero inputs, or more than one input, it reports an error.

> (abs 3+4i)
abs: contract violation. expected: real?  given: 3+4i
> (abs 'one)
abs: contract violation.  expected: real?  given: 'one
> (abs abs)
abs: contract violation.  expected: real?  given: #<procedure:abs>
> (abs)
abs: arity mismatch;
the expected number of arguments does not match the given number
  expected: 1
  given: 0
> (abs 2 3 4)
abs: arity mismatch;
the expected number of arguments does not match the given number
   expected: 1
   given: 3

Important Evaluation Criteria

We will primarily evaluate your work on correctness (does your code compute what it's supposed to and are your procedure descriptions accurate); clarity (is it easy to tell what your code does and how it acheives its results; is your writing clear and free of jargon); and concision (have you kept your work short and clean, rather than long and rambly).


Jerod Weinman

Copyright 2007-2012 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .