Docsity

Homework 1 Solved - Computer Science - Spring 2009 | CIS 252, Assignments of Computer Science

Toggle sidebar
bg1
Homework 1 CIS 252 (Spring 2009) — Introduction to Computer Science
Coverage
This assignment covers material through Chapter 3 of Haskell: The Craft of
Functional Programming.
Logistics
This homework is officially due in the bin in CST 3-212 by noon on Thursday,
January 29. However, it comes with an automatic extension: anything submit-
ted by noon on Friday, January 30 will be accepted as being on time.
You may work singly or in pairs on this assignment.
The following is based on Chapter 3 of Thompson’s HCFP.
Background
In this homework you will develop a series of little programs about lines in the
plane. We start with reviewing some high school math. Also see
http:
//mathforum.org/dr.math/faq/formulas/faq.ag2.html#twolines
for a bit more background than covered here. Also, follow Polya’s advice and
draw pictures as you are reading this.
Lines and their equations. Each line in the x-y-plane is given by an equation
of the form:
a·x+b·y+c=0,
where a,b, and care real numbers and at least one of aand bare nonzero. Some
examples:
1. The vertical line passing through the point (1,0)is given by: x−1=0
(that is, a=1, b=0, and c=−1).
2. The horizonal line passing through the point (0,−6)is given by: y+6=0
(that is, a=0, b=1, and c=6).
3. The line with slope 2.5 that passes through the point (1,0)is given by:
5·x−2·y−5=0 (that is, a=5, b=−2, and c=−5).
Note that any line is given by many equations. For example, the line of example
3 is also given by:
10 ·x−4·y−10 =0,−2.5·x+y+2.5=0,x−0.4·y−1=0, ...
Degenerate lines. An equation of the form 0 ·x+0·y+c=0 (that is, an equa-
tion with a=b=0) is called degenerate. (When c=0, then every point satisfies
the equation. When c6=0, no point satisfies the equation.) By convention, we
shall consider a degenerate equation as describing a degenerate line (that is, a
nonsense line). That way, every equation of the form a·x+b·y+c=0 names
some sort of line, even if it turns out to be nonsensical.
Horizonal and vertical lines and intercepts. Suppose the equation a·x+b·
y+c=0 is not degenerate.
•The equation describes a horizonal line exactly when a=0.
•The equation describes a vertical line exactly when b=0.
•If the line named by the equation is not horizonal, then it crosses the x-axis
at a single point, called the x-intercept. The x-coordinate of this point is:
−c/a.
•If the line named by the equation is not vertical, then it crosses the y-axis
at a single point, called the y-intercept. The y-coordinate of this point is:
−c/b.
Page 1
pf3

Partial preview of the text

Download Homework 1 Solved - Computer Science - Spring 2009 | CIS 252 and more Assignments Computer Science in PDF only on Docsity!

Coverage

This assignment covers material through Chapter 3 of Haskell: The Craft of Functional Programming.

Logistics

This homework is officially due in the bin in CST 3-212 by noon on Thursday, January 29. However, it comes with an automatic extension: anything submit- ted by noon on Friday, January 30 will be accepted as being on time.

You may work singly or in pairs on this assignment.

The following is based on Chapter 3 of Thompson’s HCFP.

Background

In this homework you will develop a series of little programs about lines in the plane. We start with reviewing some high school math. Also see

http: //mathforum.org/dr.math/faq/formulas/faq.ag2.html#twolines

for a bit more background than covered here. Also, follow Polya’s advice and draw pictures as you are reading this.

Lines and their equations. Each line in the x-y-plane is given by an equation of the form:

a · x + b · y + c = 0 ,

where a, b, and c are real numbers and at least one of a and b are nonzero. Some examples:

  1. The vertical line passing through the point ( 1 , 0 ) is given by: x − 1 = 0 (that is, a = 1, b = 0, and c = −1). 2. The horizonal line passing through the point ( 0 , − 6 ) is given by: y + 6 = 0 (that is, a = 0, b = 1, and c = 6). 3. The line with slope 2.5 that passes through the point ( 1 , 0 ) is given by: 5 · x − 2 · y − 5 = 0 (that is, a = 5, b = −2, and c = −5).

Note that any line is given by many equations. For example, the line of example 3 is also given by:

10 · x − 4 · y − 10 = 0 , − 2. 5 · x + y + 2. 5 = 0 , x − 0. 4 · y − 1 = 0 ,...

Degenerate lines. An equation of the form 0 · x + 0 · y + c = 0 (that is, an equa- tion with a = b = 0) is called degenerate. (When c = 0, then every point satisfies the equation. When c 6 = 0, no point satisfies the equation.) By convention, we shall consider a degenerate equation as describing a degenerate line (that is, a nonsense line). That way, every equation of the form a · x + b · y + c = 0 names some sort of line, even if it turns out to be nonsensical.

Horizonal and vertical lines and intercepts. Suppose the equation a · x + b · y + c = 0 is not degenerate.

  • The equation describes a horizonal line exactly when a = 0.
  • The equation describes a vertical line exactly when b = 0.
  • If the line named by the equation is not horizonal, then it crosses the x-axis at a single point, called the x-intercept. The x-coordinate of this point is: −c/a.
  • If the line named by the equation is not vertical, then it crosses the y-axis at a single point, called the y-intercept. The y-coordinate of this point is: −c/b.

Pairs of lines. Suppose we have two lines, the first named by a 1 · x + b 1 · y + c 1 = 0 and the second named by a 2 · x + b 2 · y + c 2 = 0. Suppose neither line is degenerate.

  • The two lines are parallel exactly when a 1 · b 2 = a 2 · b 1. (Note: The two equations might name the same line. For convenience we’ll say that a line is parallel to itself.)
  • These two lines intersect in a single point exactly when the lines are not parallel.
  • If the two lines do intersect, then the x and y coordinates of the point of intersection are:

x =

b 1 · c 2 − b 2 · c 1 a 1 · b 2 − a 2 · b 1

. y =

a 2 · c 1 − a 1 · c 2 a 1 · b 2 − a 2 · b 1

  • Here is how we can test whether two equations name the same line. First we check whether the two lines are parallel. If they are not parallel, then they must be different lines. If they are parallel and vertical, then they are equal exactly when they have the same x-intercept. If they are paral- lel and not vertical, then they are equal exactly when they have the same y-intercept. (Note: There are other, maybe better, ways to do this test.)

Your Problems

Here are the Haskell functions you have to define for this assignment. We have done part of the work for you in the file:

http://www.cis.syr.edu/courses/cis252/code/lines.hs

In that file you will find the beginnings of each of the following functions. What is missing for each of the functions is: (i) more examples, (ii) more tests, and (iii) the definition of the function. (Each function in the file has a silly definition that you will have to fix.) You may find it useful to read the Notes section (on the last page of this assign- ment) before you start to write code.

  1. Define a function onLine such that

onLine (x1,y1) a b c

tests whether the point (x 1 , y 1 ) is on the line named by a · x + b · y + c = 0.

  1. Define a function degenerate such that

degenerate a b c

tests whether the equation a · x + b · y + c = 0 is degenerate.

  1. Define a function horizonal such that

horizonal a b c

tests whether the equation a · x + b · y + c = 0 names a horizonal line. (We assume here that the equation is not degenerate.)

  1. Define a function vertical such that

vertical a b c

tests whether the equation a · x + b · y + c = 0 names a vertical line. (We assume here that the equation is not degenerate.)

  1. Define a function xIntercept such that

xIntercept a b c

returns the x-coordinate of the x-intercept of the line named by a · x + b · y + c = 0, when the line is not degenerate and not horizonal. If the line is degenerate or horizonal, 0.0 is returned.

  1. Define a function yIntercept such that

yIntercept a b c

returns the y-coordinate of the y-intercept of the line named by a · x + b · y + c = 0, when the line is not degenerate and not vertical. If the line is degenerate or vertical, 0.0 is returned.