Last week you used the FOR command to print the square roots of the numbers from one to fifty. You could see that the values increased more rapidly for numbers near one than for numbers near fifty. This could be made even more clear by an appropriate graph of the function.
The following procedure uses LOGO to draw a graph of the square root function, starting at x=0 and ending at the value given as an argument to the procedure.
TO GRAPHSQRT :XMAX FOR [I 1 :XMAX] [SETX :I SETY (SQRT :I)] ENDRun the procedure a few times. Can you see that the square root function increases more rapidly for smaller inputs? Do you find this graph helpful? Would you rather graph a different function? The graphing procedure isn't state transparent. Should it be? Would you like to be able to draw a graph at a different location on the screen? Would you like to be able to draw the graph with a different initial value? Would you like to add axes, or to use a different scale?
Make some improvements to the GRAPHSQRT procedure; the result should be a procedure that draws a graph that is useful to you.
The SQRT command takes an integer argument and outputs a number. You can modify your GRAPHSQRT function to graph the output of any LOGO command that behaves this way. Not surprisingly, you can also use LOGO to make bar graphs. Given an array of numbers, you can use the ITEM command to find the :I'th number in the array.
Write a procedure called BARGRAPH that takes an array of numbers as an argument and then draws a bar graph displaying the values of the numbers in the array. Here are some questions you should answer before you start writing the procedure:
You could modify the GRAPHSQRT procedure to graph any function that takes a numeric argument and outputs a number. However, you would probably prefer not to have to rewrite GRAPHSQRT every time you needed to graph a different function. Instead, you can use a procedure that takes a function as an input and draws the graph of that function!
The procedure GRAPHFUN below does this. Its arguments are the name of the function and the maximum x value to be graphed. Running the command GRAPHFUN "SQRT 200 will display the graph of the SQRT function!
TO GRAPHFUN :FUNCTION :XMAX
FOR [I 1 :XMAX] ~
[SETX :I ~
SETY RUN (SENTENCE :FUNCTION :I)] ; Writes and runs a LOGO command.
END
Can you guess how this procedure works by reading it? The
SENTENCE function is used to make a list containing the LOGO
procedure name stored in :FUNCTION and the value of the
variable :I, then the RUN instructs LOGO to
interpret this list as a list of commands. Make any improvements you
like to the GRAPHFUN procedure.Here we used a LOGO procedure as an argument to another LOGO procedure. This sort of blurring of the lines between program and data is where the modern practice of object oriented programming got its start. Much of the software in development today is written in one of the object oriented languages Java or C++.