Functional Problem Solving (CSC 151 2013F) : Assignments

Assignment 2: Starting Scheme


Due: 10:30 p.m., Tuesday, 10 September 2013

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/~rebelsky/Courses/CSC151/2013F/partners/assignment.02.txt. 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 http://bit.ly/151hw2pro 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 http://bit.ly/151hw2epi 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.02 Assignment 2: Starting Scheme 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. You may not be able to figure all of them out. Make sure to try a variety of values for each procedure, including positive and negative, integer and real. Include illustative samples of the use of each procedure.

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

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


Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2013 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.