- Define the following recursive Logo procedure:
to countdown :number
if equalp :number 0 [stop]
This procedure is supposed to look like this when run:
? countdown 5
Try it -- it doesn't work! Use the Logo menu to stop the procedure.
Why doesn't the procedure work?
- Edit the countdown procedure so that it works as intended.
- Write a recursive procedure sumup that takes a positive integer n as input and
outputs the sum 1 + 2 + 3 + ... + n. You may find it helpful to review the initials.recursive procedure from Lecture 13.
- Write a procedure countup that does the opposite of procedure countdown,
as shown below. (The example at the top of page 140 in
chapter 7 may or may not be helpful.)
? countup 5
- Write a procedure slant which takes a word as input
and produces the output described on page 161.
- Fibonacci numbers are defined on page 96. Write a recursive
procedure recursefib that accepts a positive integer n
as input and outputs Fn. You might use a stop
condition that looks like:
if :num = 0 [output 1]
if :num = 1 [output 1]
For more information on special stop rules, see p. 165. Recursive
operations are ones which output a value; these are
described in more detail on pages 202-203.
- Compare your recursefib to the procedure fib
defined on page 97. Be sure to test both procedures on inputs like
23, 25 and -1. Which procedure works better? List a few significant
differences between the procedures (I'll be looking for 3 key
differences when grading.)
- Study the example procedure inout on page 144 and use
what you learn to write a recursive procedure recursepower
that works as well as the power procedure defined on page 96.
Write an instruction or a procedure that prints the multiples of 12 from 12 thru 144. Use iteration.
- In Assignment 3 we eliminated duplication of code in a procedure by writing a helper procedure that takes one input, using repeat to further streamline the code:
to triangle :sidelength
repeat 3 [forward :sidelength right 120]
A couple of classmates pointed out that you can get a cool design with
repeat 12 [nestedTriangles]
. In this exercise, rewrite nestedTriangles as nestedTriangles2, a procedure that takes an input. You should make it so that
does the same thing as TwinTriangles. Next write a procedure that calls TwinTriangles2 twelve times from within a for or while loop, making each set of nested triangles a little smaller than the one before. The pattern will look somewhat like a snail shell.
- A rock is thrown upward from the surface of Mars with an initial velocity of 10 m/s. Its height t seconds later is s(t) = 10 t - 1.86t^2 m. How high is the rock at times 0, 1, 2, 3, 4, 5, and 6 seconds? Write a procedure that shows the height of the rock at times 0, 1, 2, 3, 4, 5, and 6 seconds. Use a for loop, and use the operation position that you wrote in Assignment 4 as a helper procedure.
- Write a recursive procedure that prints Type in a number. If the user types in something other a number, the user must again be asked Type in a number. until the user eventually types in a number. At that point, the program can do anything of your choosing. (One possibility is to have it simply print Thank you.) Hint: use numberp.