to double.space
print [ ]
print
[ ]
end
The body of this definition
could just as well be written
out - with its two instructions on one line:
print [ ] print
[ ]
On the other hand splitting an instruction like print
[ ] across two lines, e.g:
print
is NOT PERMITTED!
As it happens, getting LOGO to repeat an instruction or even a series of instructions is not difficult. There is a primitive command repeat which requires two inputs. The first input must be a number (or an expression evaluating to a number) the second must be a list containing one or more instructions. With repeat we could replace the body of double.space in the definitions above by the single instruction
repeat 2 [print [ ] ]
And, changinging the name of the procedure appropriately, we could replace repeat 2 with repeat 3 or repeat 4 ...
Like procedures, inputs have names. If we call this new procedure line.space and we use the name "times for the input, the first line of the definition will be written as:
to line.space :times
It is important to understand the role of the input name set up by this line. It is the name for a particular type of variable. Our goal is that any user of line.space will be able to say line.space 3 or line.space 25 or line space any.other.expression (as long as any.other.expression is or evaluates to a number). But clearly the instructions making up the procedure cannot refer to any one (still less all!) of these numbers if they are to be formulated in the maximally general way. Instead, in order to achieve the necessary generalisation, the input number - whatever it is - must be represented within the body of the definition by the name of some variable whose current value depends on the input provided when the procedure is invoked. The function of our new first line, therefore, is to declare that the variable name to be assigned to the one (and only) input to the procedure called line.space is the word "times. A rough English paraphrase of the first line might consequently be:
If you are asked to line.space, take the first (and only) input object you are given, call it "times and then do the following . . .
The procedure body which
follows this first line need now to be no more complex than:
repeat
:times [print [ ] ]
As long as the variable
"times has some number as its value, repeat
will happily carry out
the instruction in its second input exactly that number of times.
If finally
we add the required last line end, the complete definition
becomes:
to line.space :times
repeat :times [print
[ ] ]
end
E-mail: ron.brasington@rdg.ac.uk