As we have often remarked, one great thing about computers is that they can perform repetitive tasks very quickly and accurately. We've already seen how this can be done using recursion. Another way of doing this uses the FOR command, as demonstrated in the PRINTCOUNT procedure below:
TO PRINTCOUNT :NUMBER FOR [I 1 :NUMBER] [PRINT :I] END PRINTCOUNT 7In general, the FOR command takes two lists as arguments. The first list gives it the name of a variable to increase (called an index variable), a starting value for that variable and an ending value for the variable. The second argument is just a list of commands. So most FOR commands will look something like:
FOR [VARIABLE :START :END] [command list]
The commands in the command list may or may not make use of a variable named :VARIABLE whose value changes from the number :START to the number :END, increasing by increments one each time the list of commands is run. Notice that there is no colon in front of the name of the index variable in the FOR command.
Write a procedure that uses the FOR command to to print the square roots of the numbers between 1 and 10. Modify it to do the same for the numbers from 1 to 50. Take a minute to look at the output of your procedure; how fast do the square roots increase for numbers between 1 and 10? How fast do they increase for numbers near 50?
Use MAKE and FOR to write a procedure that adds the numbers from 1 to :N, where :N is an argument given to your procedure.
Last week we introduced lists in LOGO and practiced using some tools for adding to and obtaining information contained in lists. Lists are very powerful, but also very primitive by modern standards of computer programming. How would you find the third thing in a list? How would you sort the numbers in a list? Beginning programmers solve problems like these by using arrays instead of lists. (Advanced programmers must write programs to handle much larger and more complicated data sets, and often end up using data structures that behave very much like our primitive lists!)
An array is an indexed collection of things. Each member of an array can be accessed using the ITEM command and can be changed using the SETITEM command. One way to create an array is to enclose a collection of things in curly braces ({}). To view the contents of an array, use the PRINT command. For example:
PRINT {1 2 3 4 5}
PRINT (ITEM 3 {1 2 3 4 5})
PRINT (ITEM 2 {The quick brown fox jumped over the lazy dog.})
MAKE "ARRAY6 {1 2 3 4 5 6}
PRINT :ARRAY6
PRINT (ITEM 4 :ARRAY6)
SETITEM 4 :ARRAY6 2
PRINT :ARRAY6
SETITEM 4 :ARRAY6 "fox
PRINT :ARRAY6
Experiment with defining and printing arrays until you feel
comfortable with them. Then try changing some of the items in your
arrays. Write an array containing an English sentence and ask your
neighbor to provide a new subject and verb for the sentence.
Substitute those words into the original array; does the new sentence
make sense?The COUNT command tells you how many items are in an array. Try it!
PRINT (COUNT {The quick brown fox jumped over the lazy dog})
PRINT (COUNT {1 2 3 4 5 6})
The FOR command is an excellent tool for working with arrays. The following procedure replaces each number in an array with its square.
TO SQUAREARRAY :ARRAY1
PRINT :ARRAY1
FOR [I 1 [COUNT :ARRAY1]] ~
[SETITEM :I :ARRAY1 ((ITEM :I :ARRAY1) * (ITEM :I :ARRAY1)) ]
PRINT :ARRAY1
END
SQUAREARRAY {1 2 3 4 5}
Notice that the COUNT command is enclosed in square brackets
([]); if the starting and ending values of the FOR
command are the output of some LOGO command, that command must be
enclosed inside square brackets.There is an important distinction between the item numbers in the array (given by the variable :I in the procedure above) and the numbers in that position in the array (ITEM :I :ARRAY1). It might help to think of :I as an arrow pointing to a position in the array and think of ITEM :I :ARRAY1 as the number (or word or list) in that position. Pointers and arrays are a very important part of the C programming language and other languages. Confusion about pointers and arrays is one of the most common sources of error in C and Java programs, and is one big reason why this class is about LOGO and not Java.
Write a procedure that uses MAKE, FOR,
COUNT and ITEM to compute the average of the numbers
in an array.