Logo has some of the basic language features you'd expect - variables, functions, and conditionals.
Variables
Define a variable using MAKE name value. Logo normally interprets any word as a procedure call, so to use a literal word you can prefix it with a double quote.
MAKE "A 8
You can use a variable by prefixing the name with a colon:
PRINT :A
8
Variables created with MAKE are global. The only local variables in Logo are procedure arguments.
Functions (Procedures)
Logo functions are called procedures, and they're defined like this:
TO HELLO
PRINT "HI
END
TO specifies the name of the function, the lines after are the function body, and it's all closed out with END. You can call a procedure by typing its name. You can also call a procedure from within another procedure. Logo handles recursion, so a procedure can call itself!
To run it, just type the procedure name:
?HELLO
HI
Procedure arguments are defined by listing them after the procedure name:
TO HEY "NAME
PRINT "HI
PRINT :NAME
END
If you want to delete a procedure, use the ERASE command.
ERASE "HEY
Conditionals
Logo's IF
statement is straightforward: IF test list1
If test is true, run list1. You can add another list to the end (IF test list1 list2
) and list2 will be run if test is false.
You can break this down a bit using TEST, IFTRUE, and IFFALSE. TEST test will save the result of test and use it for any calls to IFTRUE and IFFALSE.
These are equivalent:
IF :A=8 [PR [A IS 8]] [PR [A IS NOT 8]]
TEST :A=8
IFTRUE [PR [A IS 8]]
IFFALSE [PR [A IS NOT 8]]
Example
Let's put these together and draw something. First we'll define a square function:
TO SQUARE "LEN
REPEAT 4 [FD :LEN RT 90]
END
Try it out! SQUARE 50
How about a hexagon?
TO HEXAGON "LEN
REPEAT 6 [FD :LEN RT 60]
END
Now let's put them together and draw something.
REPEAT 20 [IF RANDOM 2=1 [SQUARE 40] [HEXAGON 30] RT 18]
RANDOM num will output a random number from 0 to num. Here we use it to decide whether to draw a square or a hexagon.
That's a pretty simple example, see what you can do with Logo! There are a lot more features and we can't cover them all here, check out the Logo manuals in Resources for a full reference.
Page last edited March 08, 2024
Text editor powered by tinymce.