Major Assignment 1 2017
Major Assignment 1 2017
One of the things that makes Scheme different from other common programming lan-
guages is the ability to operate with higher-order functions, namely, functions that manip-
ulate and generate other functions. This problem set will give you extensive practice with
higher-order functions, in the context of a language for graphing two-dimensional curves
and other shapes.
(define (compose f g)
(lambda (x)
(f (g x))))
Thus, for example (compose square log) is the function that returns the square of the
logarithm of its argument, while (compose log square) returns the logarithm of the square
of its argument:
(log 2)
;Value: .6931471805599453
Just as squaring a number multiplies the number by itself, thrice of a function composes
the function three times. That is, ((thrice f) n) will return the same number as (f(f(f
n))):
(define (thrice f)
(compose (compose f f) f))
((thrice square) 3)
;Value: 6561
(define (identity x) x)
(define (repeated f n)
(if (= n 0)
identity
(compose f (repeated f (- n 1)))))
(sin(sin(sin(sin(sin 3.1)))))
;Value: 4.1532801333692235e-2
2
To work with points, we need a constructor, make-point, which constructs points from
numbers:
(define (make-point x y)
(lambda (bit)
(if (zero? bit) x y)))
WE also define selectors, x-of and y-of, for getting the x and y coordinates of a point.
Here is one way to do this without cons car and cdr.
For example, we can define the curve unit-circle and the curve unit-line (along the
x-axis):
(define (unit-circle)
(lambda (t)
(make-point (sin (* 2 pi t))
(cos (* 2 pi t)))))
(define (unit-line-at y)
(lambda (t) (make-point t y)))
Q1. Define a function (vertical-line p l) with two arguments, a point and a length,
that returns a vertical line of length l beginning at the point p. Thus you should
be able to use this as ((vertical-line (make-point 2 3) 5) .25), which means
make a vertical line of length 5 starting at point (2,3), and return the point lying at
the parameter value .25.
defines a function which takes a curve and transforms it into another, rotated, curve.
3
Q2. You can define several useful curve-transformations. Define
(a) (reflect-through-y-axis curve), which turns a curve into its mirror image.
Thus you should be able to say (reflect-through-y-axis unit-line).
(b) (translate x y curve), which rigidly moves a curve given distances along the
x and y axes. Again, you should be able to use this function as (translate 2
3 unit-line).
(c) (scale x y curve), which stretches a curve along the x and y coordinates by
given scale factors, and
(d) (rotate-around-origin radians curve) returns a Curve-Transform which ro-
tates a curve by a given number of radians.
(e) A convenient Curve-Transform is (put-in-standard-position curve). Well
say a curve is in standard position if its start and end points are the same as
the unit-line, namely it starts at the origin, (0, 0), and ends at the point (1, 0).
We can put any curve whose start and endpoints are not the same into standard
position by rigidly translating it so its starting point is at the origin, then rotating
it about the origin to put its endpoint on the x axis, then scaling it to put the
endpoint at (1, 0).
It is useful to have operations which combine curves into new ones. We let Binary-Transform
be the type of binary operations on curves. The function connect-rigidly is a sim-
ple Binary-Transform. Evaluation of (connect-rigidly curve1 curve2) returns a curve
consisting of curve1 followed by curve2; the starting point of the curve (connect-rigidly
curve1 curve2) is the same as that of curve1 and the end point is the same as that of
curve2.
Q3. There is another, possibly more natural, way of connecting curves. The curve returned
by (connect-ends curve1 curve2) consists of a copy of curve1 followed by a copy
of curve2 after it has been rigidly translated so its starting point coincides with the
end point of curve1. Write a definition of connect-ends.
3 Drawing Curves
To draw curves in DrRacket, copy the lines given below at the begining of the definition
area:
(require plot)
(plot-new-window? #t)
(plot-width 900)
4
(plot-height 900)
4. Write a function (gosper-step curve), which given a curve, would give the curve
after a single gosper-step. You can assume that the input curve is in the standard
position, and the curve returned by gosper-step should also be in the standard
position.
5
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
6
5. Now using gosper-step, write a function (gosper-curve level) which will draw
the gosper curve for the given level. (gosper-curve level) should use the function
repeated mentioned earlier.
6. We can generalize the gosper curve in two ways. First, the starting curve need not
always be a unit-line. If can be any curve in the standard position. Secondly, the
angle of rotation also need not be fixed. It can change from one level to another.
Write a function called (gosper-general curve calc-angle l), which will generate
a l-level gosper-curve with curve as the starting curve, and the angle at each level
obtained by applying calc-angle to level. You should be able to produce the
gosper curves shown in the example by calling gosper-general as (gosper-general
unit-line (lambda (theta) (/ 3.1415 4)) 8).
We can now invent other schemes like the Gosper process, and use them to generate
fractal curves.
7. The (koch-curve level) is produced by a process similar to the Gosper curve. We show
once again the zeroth, first, second, third, fourth and eighth approximations to the
Koch curve. Write a function (koch-curve level) that generates Koch curves at
different levels.
8. Repeat the process for the Sierpinski triangles, whose zeroth, first, second, third,
fourth and eighth approximations are shown in the figure below. Call the function
(sierpinski-curve level).
7
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
1 1
y axis
y axis
0 0
0 1 0 1
x axis x axis
8
1.5
1.5 1.5
1.5
11 11
axis
axis
yy axis
yy axis
.5
.5 .5
.5
00 00
-.5
-.5 -.5
-.5
-.5
-.5 00 .5
.5 11 1.5
1.5 -.5
-.5 00 .5
.5 11 1.5
1.5
xx axis
axis xx axis
axis
1.5
1.5 1.5
1.5
11 11
axis
axis
yy axis
yy axis
.5
.5 .5
.5
00 00
-.5
-.5 -.5
-.5
-.5
-.5 00 .5
.5 11 1.5
1.5 -.5
-.5 00 .5
.5 11 1.5
1.5
xx axis
axis xx axis
axis
1.5
1.5 1.5
1.5
11 11
axis
axis
yy axis
yy axis
.5
.5 .5
.5
00 00
-.5
-.5 -.5
-.5
-.5
-.5 00 .5
.5 11 1.5
1.5 -.5
-.5 00 .5
.5 11 1.5
1.5
xx axis
axis xx axis
axis