Download Programming using the GeomLab language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Large numbers wikipedia , lookup

Big O notation wikipedia , lookup

Collatz conjecture wikipedia , lookup

Location arithmetic wikipedia , lookup

Function (mathematics) wikipedia , lookup

History of the function concept wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Series (mathematics) wikipedia , lookup

Elementary mathematics wikipedia , lookup

Factorial wikipedia , lookup

Non-standard calculus wikipedia , lookup

Addition wikipedia , lookup

Transcript
Programming using the GeomLab language
1
Let expressions
It is often useful when writing program to be able to define values for variables
that we may want to use. We can use the command let to do this. The
expression
let a = 3 in a + 4;
gives 7 as the result. The let command gives a value to the variable a (in our
case the value 3) which can then be used later. If we want to store more than
value then we can use more than one let. For example
let a = 5 in let b = 6 in a*b;
gives the answer 30.
Question 1.1. Try to work out the answers to these let expressions and then
use GeomLab to check your answers:
(a) let a = 4 in a*(a+1);
(b) let b = 5 in 6;
(c) let c = 10 in let d = 12 in d - c;
(d) let e = 3 in let f = e + 4 in e*f;
(e) let g = 4 in let h = g-1 in g+2;
(f) let n = 5 in let n = 6 in n;
(g) let p = 6 in let p = p-1 in p;
(h) let x = y + 1 in let y = 5 in x+y;
We can also use let to make expressions clearer. For example, the area of
a circle is given by the formula area = πr2 . If the radius is r = 14 and we take
π ≈ 22
7 then we could write
let r = 14 in let pi=22/7 in pi*r*r;
giving us the answer 616. We can easily use this expression with a different
radius.
Question 1.2. Write an expression in GeomLab to work out the area of a circle
with radius 9 and take π ≈ 3.14.
1
For a quadratic equation ax2 + bx + c = 0 we can find the solutions of this
equation by using this formula:
√
−b ± b2 − 4ac
x=
2a
How can write a function which works out this?
One possible function is:
define quad(a,b,c) = [(-b + sqrt((b*b) - (4*a*c)))/(2*a),
(-b - sqrt((b*b) - (4*a*c)))/(2*a)];
Question 1.3.
(a) Write this function into GeomLab and check that quad(1,-3,2) gives the
answer [2,1]. Is this the correct answer?
(b) Use the function quad to find the solutions to the equations x2 +2x−15 = 0
and 2x2 + 7x − 4 = 0. Check that your answers are correct by substituting
the values back into the equations.
(c) Try computing quad(1,2,3). What happens? Why is this?
From our definition of quad we can see that the expression b2 − 4ac, called
the discriminant, is calculated twice.
Question 1.4. Rewrite the definition of quad to put the discriminant in a let
expression. So your new definition will look like:
define quad(a,b,c) = let d = ... in ...
Check your answers to the last question using this new function.
You will seen that if we try to compute quad(1,2,3) we get an error message
telling us that we are trying to take the square root of a negative number. This
happens when the discriminant is less than zero and the quadratic equation has
complex roots. We can put a test into our definition of quad to check whether
the discriminant is negative:
define quad(a,b,c) = let d = (b*b) - (4*a*c) in
if d < 0 then "negative discriminant"
else [(-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a)];
Question 1.5. Look at this definition for quad
define quad(a,b,c) = let d = (b*b) - (4*a*c) in
if d < 0 then "negative discriminant"
else (let e = 2*a in let f = (-b/e) in
let g = sqrt(d)/e in [f+g, f-g]);
Show that this definition works out the same expression as before.
GeomLab Programming
Stephen Drape, OUCL
2
Consecutive Numbers
Suppose that we want to print out a list of the first five positive integers (whole
numbers). This is quite easy:
[1,2,3,4,5]
But what if we wanted to print out a list of the first hundred positive integers?
It would be impractical to just write a list by hand and so instead we should
write a function that will do this for us. The list [1, 2, 3, 4, 5] is built up as
follows:
[1, 2, 3, 4, 5] = 1 : [2, 3, 4, 5] = . . . = 1 : 2 : 3 : 4 : [5]
So to build the list for the 1 to 5 we have to add 1 on the front of the list for
2 to 5 rather than just adding an extra number (5) onto the end of a previous
list ([1,2,3,4]). In fact it is easier to write a function that does a more general
calculation: that is a function that prints out a list of consecutive numbers
starting from any integer (and not just one).
So we would like a function
consec(a,b)
that will print out the consecutive integers starting at a and finishing at b. Thus,
consec(1,5) should print [1,2,3,4,5].
How do we write this function? Well,
consec(1,5) = [1,2,3,4,5] = 1:[2,3,4,5] = 1:consec(2,5)
So, in general, we have that
consec(a,b) = a:consec(a+1,b)
Note that for any number b
consec(b,b) = [b]
This leads us to the following definition
define consec(a,b) = if a=b then [b]
else a:consec(a+1,b);
Question 2.1.
(a) Type in the above definition of consec and check that consec(1,5) gives
the correct answer.
(b) Try examples, such as consec(1,100), consec(8,29), consec(7,7) and
consec(-4,4).
(c) What happens with consec(5,1)?
For the expression consec(5,1) we should just get the empty list [] as the
answer since there isn’t an increasing list of consecutive numbers which starts at
5 and ends at 1. Instead what happens is that our function does not terminate
as the start number keeps increasing and will never reach the number 1. Thus
we will need to change the program so that it gives an empty list when the first
number is bigger than the second number.
GeomLab Programming
Stephen Drape, OUCL
Question 2.2. Complete the new definition for consec:
consec(a,b) = if ... then [] else ...;
Check that your new definition gives the correct answers.
We have seen how to produce a list of consecutive integers but what if we
want a list of odd numbers or multiples of 3? For this we will have to write a
new function which takes in a start and finish number as before but will take in
a step value. The step value tells us how much to increase the start number by
— so if we wanted odd numbers then we would have a step of 2, for multiples
of 3 then the step value will be 3. Note that for the consec function we just
took the step value to be 1.
Here is a function that will give us a sequence of numbers using a step value:
define seq(a,step,b) = if a>b then []
else a:seq(a+step,step,b);
Question 2.3.
(a) Check the seq(1,2,19) gives the first 10 positive odd numbers. What
expression would print the first 10 positive even numbers?
(b) What expression would print out all the 2 digit numbers which are multiples of 4?
(c) What does seq(1,2,20) give? Is this correct?
(d) Check that seq(1,1,10) gives the same answer as consec(1,10).
(e) We can also use decimals numbers as well as whole numbers and so try
seq(1,0.5,3) and seq(0,0.1,1).
(f) What should be printed for seq(10,-1,1) and seq(1,0,2)? What happens?
Up to now we have considered sequences of numbers that increase but what
about printing out a list of numbers that decrease? We could easily write a new
version of seq that prints out a decreasing list of numbers:
define seqdown(a,step,b) = if a<b then []
else a:seqdown(a-step,step,b);
So, seqdown(10,2,0) prints out [10,8,6,4,2,0].
But can we instead modify our definition of seq so that it can handle decreasing sequences? That is we would like seq(10,-2,0) to print out the same
values as seqdown(10,2,0). If we try to compute seq(10,-2,0) then all that
is printed is the empty list [] because the start number 10 is bigger than the
end number 0. Thus if the step value is negative then to finish we need to have
the start value be less than the end value (because the sequence is counting
down). So we could write the following:
define seq(a,step,b) = if step < 0 and a<b then []
else if step > 0 and a>b then []
else a:seq(a+step,step,b);
GeomLab Programming
Stephen Drape, OUCL
Check that this new version works correctly.
When we have a number of conditions such as in the latest version of step
it is often hard to tell what exactly is going on and it very easy to make a
mistake. Can we rewrite this definition so that we only have one condition to
check? Both conditions have a test for step and a test involving a and b. What
is the value of the expression a − b in both of those conditions? Well, when step
is negative then so is a − b and when step is positive then a − b is positive as
well. Note that we cannot just have this test: a − b = step because it tests to
see whether the values are exactly equal and not whether they are both positive
(or negative). Instead, let us consider the expression (a − b) × step. Here’s a
table of the signs — i.e. whether they are positive (+ve) or negative (-ve):
(a − b)
+ve
+ve
-ve
-ve
step
+ve
-ve
+ve
-ve
(a − b) × step
+ve
-ve
-ve
+ve
We can see that when the signs of step and a − b are the same then the product
is positive (and negative when they do not match). So, we can use this fact to
rewrite our definition of seq:
define seq(a,step,b) = if (a-b)*step > 0 then []
else a:seq(a+step,step,b);
Question 2.4. Check that this new definition of seq works correctly.
As an alterntive we can define a function that takes a start number, a step
value and the number of items in the list:
define seqn(a,step,n) = if n>0 then a:seq(a+step,step,n-1)
else []
Question 2.5. (Harder!) The sequences that we’ve considered so far increase
(or decrease) by adding a constant step value — this type of sequence is called
an arithmetic sequence. A geometric sequence is one in which we multiply
each values by a fixed value — so this sequence 1, 2, 4, 8, 16, ... (powers of 2) is
a geometric sequence.
(a) Write a GeomLab function gseqn(a,m,n)that will produce a geometric
sequence by taking a start value a, a multiplier m and the number n of
items in the list.
(b) Check that gseqn(1,2,10) produces
[1,2,4,8,16,32,64,128,256,512]
(c) What values in gseqn will produce the sequence below?
[4,2,1,0.5,0.25,0.125]
(d) What about this sequence?
[16, -24, 36, -54, 81, -121.5]
GeomLab Programming
Stephen Drape, OUCL
3
Sums and Sequences
What is the sum of the first 10 positive integers? (The Mathematical word
integer means “whole number”.) You should be able to do this quite quickly
(with or without a calculator) and get an answer of 55. But what is we wanted
to find the sum of the first 100 or even the first 1000 positive integers? Can
we write a computer program to do this? We would like to define a function
sum(n) that gives us the sum of the first n positive integers.
Let us look at how we would work out sum(3):
sum(3) = 3 + 2 + 1
and for sum(4) we would have
sum(4) = 4 + 3 + 2 + 1
We can use the result for sum(3) to get:
sum(4) = 4 + sum(3)
So in general, we have
sum(n) = n + sum(n − 1)
This is called a recursive definition because sum is defined in terms of itself.
So we should be able to write:
define sum(n) = n + sum(n-1)
Question 3.1.
(a) Type in the definition of sum
(b) Try an example, such as sum(4). What happens? Why do think this is?
When trying an example you will have found that you get an error message
telling you that the calculation takes too many steps — what is happening? Let
us look at the definition of sum(n) again. For sum(1), we have:
sum(1) = 1 + sum(0)
and then:
sum(0) = 0 + sum(−1)
sum(−1) = −1 + sum(−2)
and so on. This calculation will never terminate because we have not put in
a definition for sum(0) which we want to be 0 (these types of definitions are
called base cases). So our new definition of sum is:
define sum(n) = n + sum(n-1) when n>0
| sum(0) = 0
(To get a newline you hold down CTRL and then press ENTER.)
GeomLab Programming
Stephen Drape, OUCL
Question 3.2.
(a) Type in this definition and check that it terminates with the correct answer.
(b) What is the answer to sum(100)?
(c) Find the sum of the first 1000 positive integers.
(d) Find the sum of the integers between 101 and 1000 (inclusive).
So we have seen how we can write a program that allows us to sum up
consecutive integers but how could we write a function sumEven(n) to add the
first n positive even numbers?
Let us look at sumEven(3) and sumEven(4):
sumEven(3) = 6 + 4 + 2
sumEven(4) = 8 + 6 + 4 + 2
= 8 + sumEven(3)
= (2 × 4) + sumEven(3)
So, in general, we have that:
sumEven(n) = (2 × n) + sumEven(n − 1)
So, remembering to include a base case, we can write the following function:
define sumEven(n) = (2 * n) + sumEven(n-1) when n > 0
| sumEven(0) = 0
Question 3.3.
(a) Type in the function for sumEven(n) and check that the sum of the first
100 positive even numbers is 10100.
(b) Write a function sumOdd(n) that will calculate the sum of the first n
positive odd numbers. Check that sumOdd(10) is 100.
(c) Can you write sumOdd(n) in terms of sum and sumEven?
(d) Can you write sumOdd(n) just using the function sum?
(e) Work out a few results for sumOdd(n) using different values of n. Do you
notice anything about your answers?
Question 3.4. (Harder!)
(a) Use the function:
define square(x) = x * x
to write a function sumSq(n) that finds the sum of the squares of the first
n positive integers. Check that sumSq(11) = 506.
(b) Write a function sumCube(n) that works out the sum of the cubes of the
first n positive integers. Check that sumCube(7) = 784.
GeomLab Programming
Stephen Drape, OUCL
(c) Work out sumCube(6) and square(sum(6)). What do you notice? Is this
true for all positive integers?
(d) Write a function product(n) that finds the product of the first n integers.
(This function is known as the factorial function.) So, for example,
product(5) = 5 * 4 * 3 * 2 * 1
Check that product(5) = 120 and product(10) = 3628800.
We have seen how we can add up a sequence of numbers that follow a certain
pattern (e.g. consecutive, odd, multiples of 3) but how can we write a function
that adds up a list of any numbers? Suppose we want to sum up the numbers
in the list [10, 21, 13, 7]. Before we can write a summing function we need to
know how lists are defined.
A list can either be empty ([ ]) or be of the form x : xs where x is the first
element of the list, xs is another list and : is an operation called “cons”. This
is another recursive definition as we define the list x : xs using another list xs.
Recursive definitions are quite common in Maths and Computer Science. We
can define natural numbers (that is whole numbers that are non-negative) as
follows:
a natural number is either 0 or one more than another natural number
So the list [10, 21, 13, 7] is built up as follows:
[10, 21, 13, 7] = 10 : [21, 13, 7] = . . . = 10 : 21 : 13 : [47] = 10 : 21 : 13 : 7 : [ ]
When defining functions on lists, we have to have a case for the empty list
(the base case) and a case using cons. So we can define sumList as follows:
define sumList([ ]) = 0
| sumList(x:xs) = x + sumList(xs)
Question 3.5.
(a) Type in the definition of sumList.
(b) Check that sumList([10,21,13,7]) gives 51 as the answer.
(c) Check the function with some more lists of numbers which include negative
and decimal numbers.
(d) Define a function prodList that works out the product of a list of numbers.
(e) How could we define a function length that works out the length of a list?
(f) Write a function that works out the mean (average) of a list of numbers.
GeomLab Programming
Stephen Drape, OUCL
4
Drawing patterns
We will look at using the following two shapes:
straight
bend
to produce some patterns which are defined recursively.
As a start, we will draw an addition sign.
To draw this we will need to use the function rot which rotates a shape by
90◦ and rot2 which does rot twice and rot3 which does three rotations. Here
is the start of the definition:
define plus = (blank $ bend $ rot3(bend) $ blank) &
(bend $ rot2(bend) $ ... $ ...)
& (...) & (...)
Question 4.1. Complete the definition of plus.
We can see that this picture has the same L-shaped piece which is rotated
four times. We can easily write a definition for a function which can produce
a shape by taking in just the left-hand corner of the picture. This definition of
cycle is already defined in GeomLab
cycle(p) = (p $ rot3(p)) & (rot(p) $ rot2(p))
Question 4.2.
(a) What shape does cycle(bend) produce?
(b) Use cycle to produce the picture for plus.
(c) Produce some more patterns using cycle
The next shape we will aim to draw is a square
frame of a specific length. For example
square(5)
produces the square frame on the right.
This frame is made up of 4 corners (using bend) and four sides. To get the
sides, we need to define a function side(n) which produces a straight line of
length n. We can define side(n) as follows:
define side(n) = straight $ side(n-1) when n>1
| side(1) = straight
GeomLab Programming
Stephen Drape, OUCL
The function side is defined in terms of itself and so is an example of a recursive function. Notice that we write a separate expression for when n = 1. This
is called a base case and is needed so that the recursion stops. (Try removing
the base case from the definition and see what happens.)
Question 4.3. Type in the definition of side and try a few different values for
n. What do you notice about the length and width of the line as you increase
the value of n?
Using bend and side we can now define a function that draws a square
frame. The function will look something like this:
define square(n) = (bend $ side(n) $ ... )
& (... $ blank $ ...)
& (... $ side(n) $ ...)
Question 4.4.
(a) Complete the definition of square. Check that square(5) looks the same
as the picture above.
(b) Can we use the function cycle (discussed earlier) to produce a square?
We can define a function row that allows us to draw a row of pictures:
define row(n,p) = p $ row(n-1,p) when n>1
| row(1,p) = p
In this function, p stands for the picture and n for the number of pictures. For
example:
row(4,square(3))
Question 4.5. Using the definition of row to define a function grid(r,c,p)
which will produce r rows and c columns of picture p. So, for example
grid(4,3,plus)
Using a similar definition to square we can produce a function sq which
produces a picture containing squares with decreasing size. For example,
sq(6)
Question 4.6. Write a definition for sq. The definition will be similar to the
one for square except that blank is replaced by another instance of sq. You
will also have to write a base case.
GeomLab Programming
Stephen Drape, OUCL
Let us see how we can define a function which draws a rectangle, e.g.:
rectangle(3,5)
rectangle(7,2)
Question 4.7. Write a definition for rectangle so that rectangle(n,p) draws
a rectangle with width n and height p. The definition is similar to square but
the shape of the middle blank needs to be changed. You may find the function
stretch helpful.
Question 4.8. (Harder!) Define a function rect which is similar to sq but
uses rectangle instead of squares.
rect(5,3)
You may find the functions stretch and aspect useful to ensure that the
inner rectangles are of the correct shape.
Question 4.9. (Harder!) Try to define a function which produces a staircase
which can have a variable number of steps. So, for example,
stairs(4)
To produce one step, we can define:
stairs(1) = bend $ rot2(bend)
The difficultly in defining this function is making sure that the blank spaces at
the top left and bottom right of the picture are the correct sizes. For the top
left space, you might want to use the row function and for the bottom right you
might use stretch.
GeomLab Programming
Stephen Drape, OUCL
Answers
Let expressions
Question 1.1
(a) 20
(b) 6 (the value of b has no effect on the final answer as it does not contain b)
(c) 2
(d) 21 (e = 3 and f = 3 + 4 = 7)
(e) 6 (the value of h has no effect on the final answer)
(f) 6 (the second definition to n overwrites the first definition)
(g) 5 (after the first let then value of p is 6 and then after the second let
the value of p is 6 − 1 = 5)
(h) Gives an error as y is not defined in the first let
Question 1.2
let r = 9 in let pi = 3.14 in pi*r*r;
which evaluates to 254.34.
Question 1.3
(a) The expression quad(1,-3,2) should give us the solution to x2 − 3x + 2.
Trying x = 2 gives 22 − (3 × 2) + 2 = 4 − 6 + 2 = 0 X
Now trying x = 1 gives 12 − (3 × 2) + 2 = 1 − 3 + 2 = 0 X
(b) For x2 + 2x − 15 = 0, we try quad(1,2,-15) which gives [3,-5]. Trying
x = 3 in the equation gives 32 + (2 × 3) − 15 = 9 + 6 − 15 = 0 X
Now trying x = −5 gives (−5)2 + (2 × −5) − 15 = 25 − 10 − 15 = 0 X
For the equation 2x2 + 7x − 4 = 0 we try quad(2,7,-4) we get [0.5,-4]
First let’s try x = 12 in the equation, we get
(2 ×
1
1
12
1
) + (7 × ) − 4 = + 3 − 4 = 0 X
2
2
2
2
Now trying x = −4 gives (2 × (−4)2 ) + (7 × −4) − 4 = 32 − 28 − 4 = 0 X.
(c) Computing quad(1,2,3) gives an error message telling us that the calculation involves taking a negative square root. This is because the equation
x2 + 2x + 3 = 0 has no real solutions.
Question 1.4 The new definition is:
define quad(a,b,c) = let d = (b*b) - (4*a*c) in
[(-b + sqrt(d))/(2*a), (-b - sqrt(d))/(2*a)];
GeomLab Programming
Stephen Drape, OUCL
Question 1.5
for f and g
Consider the expression f + g and let us putting in the values
f + g = (-b/e) + ( sqrt(d)/e)
Now putting the values for d and e and simplifying we get
f + g = (-b/(2*a)) + ((sqrt((b*b) - (4*a*c))/(2*a))
= (-b + sqrt((b*b) - (4*a*c))/(2*a)
which is the same as for our earlier definition of quad. Similarly we can show
that f-g gives the same expression as before.
Consecutive Numbers
Question 2.1
(a) The expression consec(1,5) gives
[1,2,3,4,5]
(b) consec(1,100)
consec(8,29)
consec(7,7)
consec(-4,4)
[1,2,3,...,99,100]
[8,9,10,...,28,29]
[7]
[-4,-3,-2,-1,0,1,2,3,4]
(c) consec(5,1) fails to give an answer
Question 2.2 The definition is:
define consec(a,b) = if a>b then [] else a:consec(a+1,b)
Question 2.3
(a) seq(1,2,19) gives
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
For the first 10 positive even numbers we need to type seq(2,2,20).
(b) Try seq(12,4,96) — we need to put the first 2 digit multiple of 4 as the
start number and the finish number can be any number between 96 and
99 (but not 100).
(c) This gives
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
which is correct even though the sequence finishes at 19 (and not 20).
(d) Both should give
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
GeomLab Programming
Stephen Drape, OUCL
(e) seq(1,0.5,3) gives
[1, 1.5, 2, 2.5, 3]
and seq(0,0.1,1) gives
[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
(f) seq(10,-1,1) just prints out the empty list [] because the start value is
bigger than the end value immediately and seq(1,0,2) fails to give an
answer as it never terminates.
Question 2.4 Trying seq(10,-1,1) now gives
[10,9,8,7,6,5,4,3,2,1]
Question 2.5
(a) One possible definition is:
define gseqn(a,mult,n) = if n>0
then a:gseqn(a*mult,mult,n-1)
else [];
(c) gseqn(4,0.5,6)
(d) gseqn(16,-1.5,6)
Sums and Sequences
Question 3.1
(b) Typing in sum(4) gives the following error
Aargh: sorry, that took too many steps
The explanation of why this happens can be found in the text.
Question 3.2
(a) Trying, for example, sum(4) gives 10.
(b) 5050
(c) 500500
(d) 495450 this can be worked out by doing
sum(1000) - sum(100)
GeomLab Programming
Stephen Drape, OUCL
Question 3.3
(b) One possible definition is:
define sumOdd(n) = (2*n-1 ) + sumOdd(n-1) when n>0
| sumOdd(0) = 0
(c) We can write
define sumOdd2(n) = sum(2*n) - sumEven(n)
(d) Since sumEven(n) = 2 * sum(n) (why?) then we can define
define sumOdd3(n) = sum(2*n) - (2*sum(n))
We can also define
define sumOdd4(n) = 2*sum(n) - n
(e) For example, sumOdd(20) = 400 = 202 and sumOdd(12) = 144 = 122 .
So in fact,
sumOdd(n) = n2
Question 3.4
(a) We can define
define sumSq(n) = square(n) + sumSq(n-1) when n>0
| sumSq(0) = 0
(b) We first do define cube(x) = x * x * x and then
define sumCube(n) = cube(n) + sumCube(n-1) when n>0
| sumCube(0) = 0
(c) Both sumCube(6) and square(sum(6)) give an answer of 784. In general,
for all positive integers n:
sumCube(n) = (sum(n))2
(d) A possible definition is
define product(n) = n * product(n-1) when n>0
| product(0) = 1
GeomLab Programming
Stephen Drape, OUCL
Question 3.5
(d) Here is one possible definition:
define prodList([ ]) = 1
| prodList(x:xs) = x * prodList(xs)
(e) The definition is very similar to the one for sumList:
define length([ ]) = 0
| length(x:xs) = 1 + length(xs)
(f) Using the definition of length from part (e), we can write
define mean(xs) = sumList(xs)/length(xs)
Drawing patterns
Question 4.1
define plus =
&
&
&
(blank $ bend $ rot3(bend) $ blank)
(bend $ rot2(bend) $ rot(bend) $ rot3(bend))
(rot(bend) $ rot3(bend) $ bend $ rot2(bend))
(blank $ rot(bend) $ rot2(bend) $ blank)
Question 4.2
(a) cycle(bend) produces a square
(b) cycle((blank $ bend) & (bend $ rot2(bend)))
Question 4.3 As the value of n increases the width decreases but the length
stays the same.
Question 4.4
(a) define square(n) = (bend $ side(n) $ rot3(bend) )
& (rot(side(n)) $ blank $ rot(side(n)))
& (rot(bend) $ side(n) $ rot2(bend))
(b) We could define
define square2(n) = cycle(((bend $ side(n)))
& (rot(side(n)) $ blank))
but actually square2(n) = square(2*n) and so this function could not
produce a square equivalent to square(5) for example.
Question 4.5
define grid(r,1,p) = row(r,p)
| grid(r,c,p) = row(r,p) & grid(r,c-1,p)
GeomLab Programming
Stephen Drape, OUCL
Question 4.6 Here is one possible definition:
define sq(1) =
| sq(n) =
&
&
(bend $ rot3(bend)) & (rot(bend) $ rot2(bend))
(bend $ side(n) $ rot3(bend))
(rot(side(n)) $ sq(n-1) $ rot(side(n)))
(rot(bend) $ side(n) $ rot2(bend))
Question 4.7 We can define rectangle as follows:
define rectangle(n,p) =
(bend $ side(n) $ rot3(bend))
& (rot(side(p)) $ stretch(n/p,blank) $ rot(side(p)))
& (rot(bend) $ side(n) $ rot2(bend))
Question 4.8 Here is one possible definition:
define
|
|
|
rect(1,1)
rect(n,1)
rect(1,p)
rect(n,p)
=
=
=
=
&
&
blank
side(n)
rot(side(p))
(bend $ side(n) $ rot3(bend))
(rot(side(p)) $ pic(n,p) $ rot(side(p)))
(rot(bend) $ side(n) $ rot2(bend))
For the centre, we use the following function, which makes sure that the
inner rectangles are the correct size:
define pic(n,p) = let r = rect(n-1,p-1) in
stretch(n/(p*aspect(r)), r)
Question 4.9
define stairs(1) = bend $ rot2(bend)
| stairs(n) = (row(n-1,blank) $ stairs(1))
& (stairs(n-1) $ stretch(1/(n-1),blank))
GeomLab Programming
Stephen Drape, OUCL
Extra Functions
define arm(1) = bend
| arm(n) = arm(n-1) $ straight
define spiral(1) = bend
| spiral(n) = arm(n) & (rot(arm(n-1))) $ rot2(spiral(n-1))
define zigzag(m,1) = arm(m) $ rot2(bend)
| zigzag(m,n) = flip(zigzag(m,n-1)) & (arm(m) $ rot2(bend))
define zagzig(1) = rot(straight)
| zagzig(n) = (bend $ rot2(arm(n-1)))
& (rot(arm(n-1)) $ flip(rot3(zagzig(n-1))))
GeomLab Programming
Stephen Drape, OUCL