Logo 2 ====== Introduction ------------ The language LOGO was originally designed to teach the concept that a computer will always obey the commands it is given, provided that they are of the correct form and syntax. This powerful implementation of LOGO can support loops, definitions, subroutines, recursive programming etc, and has many additional useful and novel features. For this reason it provides an excellent start for anyone wishing to learn the art of computer programming and a useful stepping stone to other more complicated high and low level languages. Note: In the following text the marks "I will surround all text to be typed at the keyboard or any text appearing on the screen and the [RETURN] sign within these marks will denote the pressing of the RETURN key. Starting Up ----------- To load the program type 'NEW [RETURN]' and then 'CHAIN "LOGO" [RETURN]'. The program only takes a couple of minutes to load and when it is loaded the message: MODE (0,1,2,4 OR 5) will be printed on the screen. You now have to enter the graphics mode in which the program is to operate. For the following examples mode two is used, so type: 2 [RETURN] Now the screen will display the triangular turtle in the centre, a border and a prompt (?) at the bottom-left. Trying Some Commands -------------------- Try the following commands: FORWARD 300 (RETURN) RIGHT 90 (RETURN) The first part of each line (ie. FORWARD or RIGHT) is called the COMMAND and the second part after the space (ie. 300 or 90) is called the ARGUMENT. As can be seen from this example, when the command FORWARD is typed the TURTLE moves forward through a distance which is proportional to the ARGUMENT it is given; in the first example it moves forward 300 screen units. (NB. The screen is approximately 1280 units across and 850 units down.) In the second example it turns right 90 degrees (a right angle). In fact the argument after the space may be any valid BASIC expression such as 360/7, RND (5), 2*28-14. Try typing in the above commands but using different arguments and try using the opposite commands to the two outlined above, namely BACKWARD and LEFT respectively. Repeat Loops ------------ Try typing the following: FORWARD 300 RIGHT 90 FORWARD 300 RIGHT 90 FORWARD 300 RIGHT 90 FORWARD 300 RIGHT 90 These commands draw a square on the screen by sending the turtle forward 300 units and right 90 degrees four times. This is unnecessarily tedious as the two commands are repeated four times and it could easily be shortened by using a repeat command. REPEAT 4, FORWARD 300: R1 90 (RETURN) This command has two arguments (separated by the comma). The first is the number of times the commands are to be repeated and the second is the string of commands separated by colons. There is no limit to the number of commands which can be typed after the comma but each MUST be separated by a colon and may NOT be a second REPEAT command. Abbreviations ------------- Nearly all of the LOGO commands have an abbreviated form which can be used for speed of program entry. (Refer to the quick reference guide for a summary of all commands and their abbreviations.) (Note: The language assumes that if the first part of a line matches one of the LOGO commands then that command is the one intended (e.g. if 'LOGO' is typed the command 'LOAD' will be assumed). Defining A Word --------------- So far, as soon as you have pushed return after typing a line, the computer has obeyed the line immediately. This is called programming in direct mode. It is possible, however, to make the computer store the commands you type in its memory and only to execute them when you tell it to. To do this you have to give the computer a NAME under which the instructions will be stored and then the instructions in order. When you wish to execute the instructions you simply type the NAME you have given them and the computer will execute them one after the other. Try the following: TO SQUARE (RETURN) The TO command tells the computer that we are about to define a WORD. The argument (SQUARE) is the NAME of the WORD we are going to define. When you have done this you will notice that the screen clears and the name (SQUARE) and a number are printed at the top of the screen. The number is the number that the computer has given to the WORD you are defining (you can have up to 10 words defined at any one time, numbered 1 to 10). The screen will also display a '1:' at the top left, this means that it is waiting for the first command. Try the following: REPEAT 4, FORWARD 200:RIGHT 90 [RETURN] END [RETURN] The 'END' statement means that you have finished the command list for the word SQUARE and after you have typed this the screen will revert to its usual display of the turtle. It is easy to see which words have been defined within the LOGO by typing 'LIST [RETURN]'. The screen will then display all of the WORD names and their numbers in order. If you wish to see the list of commands under a certain name simply type 'LIST name' where name is the name of the WORD. Try: LIST SQUARE and the commands you have just typed under SQUARE will be listed on the screen. To get the computer to execute the command simply type - SQUARE [RETURN] and the computer will draw a square. The Edit Command ---------------- It is possible to rectify any mistakes made in the definition of WORD. The command used to edit an existing word is simply: EDIT name [RETURN] where 'name' is the name of the word you wish to change. When you have typed this the screen will display the name at the top and it will print out the first command at the top of the screen. If you wish to change this command simply type in the correction underneath (using the cursor keys to copy parts of the original line as required). If you do not want to change this command simply press "RETURN'. The computer will continue to offer each command in turn for changing until an 'END' statement is reached or typed in as a correction. Saving And Loading ------------------ When you have defined a word you can save it on cassette tape or disk for retrieval when you next need it. To save the commands under a word type: SAVE name [RETURN] where 'name' is the name of the WORD to be saved. The computer will ask you to 'RECORD then RETURN' as it does in BASIC and the WORD will be saved on tape. To retrieve a WORD saved on tape simply type: LOAD name [RETURN] where 'name' has the same meaning as above. The computer will search through the tape until it finds the correct word which it will then load. Deleting A Word --------------- Typing: DELETE name [RETURN] will make the computer forget the WORD with the name of 'name' and all the commands under it. Typing - DELETE ALL [RETURN] will delete all the WORDS from the computer's memory and restart the program so that it asks for a MODE. Variables --------- A variable can be likened to a letterbox in which the computer stores a number. This letterbox is labelled by a name which you give it. For example, you have to store the number 100 in a variable and you want to call the variable AMOUNT, you would type - MAKE AMOUNT = 100 [RETURN] This assigns the value of 100 to the variable called AMOUNT. You can use the number in the letterbox labelled AMOUNT instead of the number 100. For example you could now type: FORWARD AMOUNT [RETURN] RIGHT AMOUNT [RETURN] The real use of a variable is that its value can be varied. In the example above we defined the new word SQUARE. However it will only draw a square of the stated size. To define a word that would cater for any size squares you may wish to draw, the following definition would do this. DELETE SQUARE [RETURN] (This deletes the old definition TO SQUARE) REPEAT 4, FORWARD SIZE:RIGHT 90 [RETURN] END [RETURN] Now instead of always going 200 units forward the turtle will move the number of units set by the variable SIZE. MAKE SIZE=100 [RETURN] SQUARE [RETURN] will draw a square of size 100. Therefore it is only necessary to change the value of the variable outside the definition to draw squares of different sizes. The following definition (LOTS) will draw a series of ever growing squares. (This assumes the last SQUARE definition has already been entered). TO LOTS [RETURN] MAKE SIZE=50 [RETURN] REPEAT 10, SQUARE:MAKE SIZE=SIZE+10 [RETURN] END [RETURN] There is no limit to the number of variables you can have in a program but all variables must obey the rules for numeric variables in BASIC (see the USER GUIDE p. 24-26) and in addition they must be no longer than 20 characters and no shorter than 3 characters. System Variables ---------------- The system variables are the variables which are used by the LOGO program itself. Most of the names of these are two characters long so that you cannot change them within LOGO. Some however have been made longer than two characters so that you may use them within the program. The next page describes these system variables. COLOR When the turtle moves it will leave a line behind it if the pen is down. The colour of this line is controlled by the variable COLOR. The value of this variable is equivalent to the second argument of the GCOL Command in BASIC (See p. 262 in the USER GUIDE). Some legal examples of the use of this variable are: MAKE COLOR=1 [RETURN] MAKE COLOR=COLOR+1 [RETURN] MAKE COLOR=YELLOW [RETURN] DEFAULT VALUE = 7 TCOLOR This variable changes the colour in which the triangular turtle is plotted (See COLOR). DEFAULT VALUE=1 COND This variable controls the condition under which the line drawn by the turtle is plotted. (See first argument of GCOL p. 262 in the USER GUIDE). Possible conditions are AND, OR, EOR and INVERT. DEFAULT VALUE=0 TCOND As for COND but controlling the condition with which the turtle is plotted. If the following is entered the turtle will leave an impression of itself on the screen as it moves off: MAKE TCOND=0 [RETURN] MAKE TCOND=3 [RETURN] DEFAULT VALUE=3 TSIZE The size of the turtle (from centre totip in screen units). is given by this variable. DEFAULT VALUE = 80 COLOURS The colours BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE and FLASH have the values 0-8 respectively and can be used in commands eg. MAKE COLOR=YELLOW [RETURN] MAKE TCOLOR=YELLOW +1 [RETURN] FORWARD GREEN [RETURN] etc Logo 2 Examples --------------- The following routine draws a square, the size of which is set by the variable 'SIZE'. TO SQUARE [RETURN] FORWARD SIZE:RIGHT 90 [RETURN] END [RETURN] However a much more flexible routine follows that will draw a polygon with any number of sides set by the variable 'SIDES' TO POLY [RETURN] REPEAT SIDES, FORWARD SIZE:RIGHT 360/SIDES [RETURN] END [RETURN] Try the above routine with various values for the variable 'SIDES'. 3 will draw a triangle, 4 will draw a square, 5 a pentagon etc. You will notice that with about 20 sides the polygon becomes indistinguishable from a circle, so one might define a circle as: TO CIRCLE [RETURN] REPEAT 20, FO SIZE:RIGHT 18 [RETURN] END [RETURN] The following routine (WAVE) is actually made up of two half circles. This is best in graphics Mode 4. If you are not already using this Mode then typing DELETE ALL will delete all the current words and will give you the option of changing modes. TO WAVE [RETURN] REPEAT 10,FO SIZE:RI 18 [RETURN] REPEAT 10,FO SIZE:LE 18 [RETURN] END [RETURN] Once the above has been typed in try this: TO SQIGGLE [RETURN] MAKE AMOUNT=0 [RETURN] REPEAT 20, HOME:RI AMOUNT:WAVE:MAKE AMOUNT=AMOUNT+18 [RETURN] END [RETURN] This takes some time to draw the complete picture because the wave routine is made up of so many short lines. While in Mode 4, try the following. TO TWIST [RETURN] REPEAT 50, FO SIZE:RI 135:MAKE SIZE=SIZE +10 END The following routines will draw the same pattern as on the front of the LOGO 2 cassette. These are quite complex and make use of the system variables in places. Type in all three routines EYES, PATTERN and PUPILS. This also requires a SQUARE routine that draws square with sides 200 long. TO PATTERN [RETURN] REPEAT 36,RI 10:F0 50:SQUARE [RETURN] END [RETURN] TO EYES [RETURN] FO 100 [RETURN] PATTERN [RETURN] LEFT 90 [RETURN] PENUP [RETURN] FO 500 [RETURN] RI 90 [RETURN] PENDOWN [RETURN] PATTERN [RETURN] PUPILS [RETURN] END [RETURN] TO PUPILS PENUP [RETURN] MOVE 400,600 [RETURN] MAKE TCOND=0 [RETURN] MOVE 950,600 [RETURN] MAKE TCOND=3 [RETURN] MOVE 675,200 [RETURN] MAKE TSIZE=180 [RETURN] RI 180 [RETURN] END [RETURN] Now typing 'EYES' will draw the complete pattern. Again this takes some time as this is quite an intricate pattern. The following routines use colour and so should be done in Mode 2. TO CTWIST [RETURN] MAKE SIZE=50 [RETURN] MAKE COLOR=1 [RETURN] REPEAT 100,FO SIZE:RI 73:MAKE SIZE=SIZE+5:MAKE COLOR=COLOR+1:IF COLOR]5,MAKE COLOR=1 [RETURN] END [RETURN] This also uses the IF statement. Every time it draws a line it increases the colour, IF the colour is greater that 5 then it resets it to color 1 again. TO STAR [RETURN] MAKE TCOND=0 [RETURN] RI 180 [RETURN] MAKE TCOND=3 [RETURN] END [RETURN] This draws a solid star, again it makes use of system variables. Having typed the 'STAR' routine try the following. TO SOLID [RETURN] MAKE TCOLOR=1 [RETURN] MAKE TSIZE=900 [RETURN] REPEAT 15,STAR:MAKE TSIZE=TSIZE-60:MAKE TCOLOR=TCOLOR+1 [RETURN] END [RETURN] Error Messages -------------- Most error messages that appear when using LOGO2 are self explanatory. For example "NO SUCH WORD" will be printed when you try to delete a non existent word. However a few will occur that are no So obvious. NO SUCH NUMBER This will occur when LOGO 2 encounters a variable that has not had a value assigned to it. It is easy to check if a variable has been asigned a number by typing 'OUTPUT NAME where NAME is the name of the variable. TOO FEW NUMBERS This will be printed when a command is not followed by as many numbers as it expects, for example the BEEP command must have 3 numbers after it. SUBSCRIPT This occurs when defined words are calling too many defined words - that is they are nested to a depth greater than 10. I DON'T KNOW If no word follows this statement it probably means a command was preceded by a space. This also occurs when a colon follows a REPEAT command when a comma should. NO WORD SPACE Only 10 new words are allowed in LOGO 2, trying to define more than this number will cause this error. Reference --------- PENUP (PENU) Lifts the turtles pen off the 'paper' so that all subsequent movements do not draw lines. PENDOWN (PEND) Puts the turtles 'pen' down onto the paper' so that subsequent movements of the turtle will leave a line on the screen. RIGHT X (RI) Turns the turtle right by x degrees. LEFT X (LE) Turns the turtle left by X degrees. FORWARD X (FO) Moves the turtle forward by X screen units. Will draw a line if the pen is down. BACKWARD X (BA) Moves the turtle back by X screen units. Will draw a line if the pen is down. MOVE X,Y (MO) Moves the tutle to the absolute screen position X units along the horizontal and Yunits up the vertical. Draws a line if the pen is down. HIDETURTLE (HI) Makes the turtle disappear SHOWTURTLE (SH) Makes the turtle re-appear BEEP X,Y,Z (BE) Makes a sound of volume X (0-15), pitch Y (0-255) and length (0-255) in 20th of a second. CLEAR (CL) Clears the whole screen and centres the turtle turning it to face up the screen. HOME (HO) Returns the turtle to the centre facing up the screen. MAKE X=Y (MA) Assigns the value Y to variable X. e.g. MAKE SIZE=200: FO SIZE OUTPUT X (OU) Prints the value (whole numbers only) of the variable or number X at the bottom of the screen. REDIFINE X,Y (RED) Makes all traces of colour X on the screen change to colour Y. (i.e. redefines a colour) See VDU19 in the BBC User Guide DEFAULT (DEFAULT) Sets all colours back to their original definitions. PRINT (PR) Prints the following string X at the current turtle position. Does nothing in command mode but when used in the definition of a word it indicates the end of that definition. Can also be used to indicate a conditional END in conjuction with the IF statement. e.g. IF X= 4, END REPEAT X,Y (REP) X is the number of times they string of commands is to be repeated. Each command should be separated by a colon and must not be another REPEAT command. IF X,Y (IF) If X is true then do Y. X is a condition (e.g. HELLO=100) and Y is a command (e.9. FORWARD 100) e.g. IF SIZE=100,MAKE SIZE=20 or IF XPOS=200,FORWARD 300 TO (TO) Define word X LIST (X) (LIST) Without an argument this will print all defined words. With Xit will list the commands under word X EDIT X (EDIT) Change the definition of word X. Each command line will be offered in turn if 'RETURN' is pressed then that line will remain the same. If new commands are typed in then the command line will be changed to the new line. DELETE X (DELETE) Makes the computer forget the definition of the word X. SAVE X (SAVE) Saves the definition of the word X onto tape. LOAD (LOAD) Loads word X in from tape. Loading ------- Tape: CHAIN"" (RETURN) Disc: SHIFT-BREAK