CSE1729 – Introduction to Programming
February 22, 2018
Laboratory Assignment 6
Objectives
Work with pairs and lists
Activities
1. Therearetwomainsystemsofdefiningpointsonatwo-dimensionalplane.Oneconsistsofadistancefrom the origin and an angle from the positive x-axis, referred to as polar coordinates. The other, more familiar system, consists of two components corresponding to the distance along the x-axis and the distance along the y-axis from the origin, referred to as Cartesian coordinates.
r
θ
y
x
- (a) Convertingtopolarcoordinates:DefineaSCHEMEfunctionnamed(c->p p)whichacceptsapointin
the Cartesian coordinate system as a pair and returns another pair representing the same point in the
polar coordinate system. That is, if the function receives the pair (x . y) as a parameter, it should
evaluate to the pair (r . θ), where r = x2 +y2 and θ = tan−1(y ). Your function should take a x
SCHEME pair as a parameter and return a SCHEME pair. Note: the arctan function in SCHEME is named atan.
- (b) Converting to Cartesian coordinates: Define a SCHEME function named (p->c p) which accepts a point in the polar coordinate system as a pair and returns another pair representing the same point in the Cartesian coordinate system. That is, if the function receives the pair ( r . θ ) as a param- eter,itshouldevaluatetothepair(x. y),wherex=r·cos(θ)andy=r·sin(θ).Yourfunction should take a SCHEME pair as a parameter and return a SCHEME pair.
2. Youmayrecallthat,giventwopoints,(x1,y1)and(x2,y2),onecanfindtheslope,m,ofastraightlinethrough
these two points with the equation:
m = y2 − y1 x2 − x1
Furthermore, the function that defines a straight line in “slope intercept form” has the form y = m x + b where b is the y-intercept. Given the slope, m, of a line and a point, (x1, y1), on the line, one can find the y-intercept by the equation b = y1 − m x1.
Define a SCHEME function, named (y p1 p2), that takes two points, p1 and p2 (each point stored in a SCHEME pair), as parameters and evaluates to a function of one parameter that, given x, will return the corresponding y for a point on the straight line between p1 and p2.
Hint: If you want to define variables for m and b , using the variable m in the expression that defines b , you can avoid using nested let forms by using let* instead.
3. Hamming Weight The Hamming weight, named after the computer scientist Richard W. Hamming, of a string is the number of symbols that are different from the zero-symbol of the alphabet used. Define a SCHEME function, named (hamming-weight lst), which takes a list of integers and returns the number of non-zero integers in the list.
1
4. HammingDistanceIninformationtheory,theHammingdistancefunction,alsonamedafterthecomputer scientist Richard W. Hamming, gives a measure of the “difference” between two strings that have the same length. In other words, it measures the minimum number of substitutions required to change one string into the other, or the minimum number of errors that could have transformed one string into the other. Define a SCHEME function, named (hamming-distance l1 l2), which takes two lists of integers of equal length as parameters, and returns the number of positions in which the value in l1 and the value in l2 are different.
5. Scalar-Vector Multiplication Multiplying a vector by a scalar produces a vector where each component is the corresponding value in the original vector multiplied by the scalar quantity. For example:
a(x1 x2 x3)=(ax1 ax2 ax3)
Define a SCHEME function, named sv-mult, which takes a list and a value as parameters and performs
scalar-vector multiplication on them.
6. VectorAdditionAddingtwovectorsproducesavectorwhereeachcomponentisthesumofthecorrespond- ing values in the original vectors. For example:
(x1 x2 x3)+(y1 y2 y3)=(x1 +y1 x2 +y2 x3 +y3)
Define a SCHEME function, named v-add, which takes two lists and performs vector addition on them. Note:
vector subtraction is structured in the same way.
7. Thedotproductoftwolistsofnumbers(x1x2x3)and(y1y2y3)is
x1∗y1+x2∗y2+x3∗y3
DefinearecursiveSCHEMEfunction(dot x y)thattakestwolistsofnumbersasitsinputsandreturnsthe dot product of those two lists. Do not use the in-built map function. You can assume the two lists have the same length.
2