Introduction to Logo #
What is Logo? #
Logo is a family of programming languages known for its use of a simple, approachable syntax and its emphasis on procedural programming and graphics. It’s often used as an introductory programming language because it allows beginners to quickly create visually engaging programs, like drawing shapes and patterns with a “turtle” graphics cursor. Instead of focusing on complex syntax, Logo prioritizes clear, step-by-step instructions, making it easier to grasp fundamental programming concepts like sequencing, iteration (loops), and recursion. While originally designed for educational purposes, Logo’s principles have influenced the design of other programming languages and continue to be relevant in various computational thinking applications.
Why Learn Logo? #
Learning Logo offers several benefits:
- Ease of Learning: Its simple syntax and turtle graphics make it significantly easier to learn than many other programming languages. Beginners can quickly see tangible results, boosting motivation and confidence.
- Foundation for Further Learning: Mastering Logo’s core concepts, such as procedures, variables, and control structures, lays a strong groundwork for learning more advanced programming languages. The logical thinking and problem-solving skills acquired are transferable.
- Visual Feedback: The immediate visual results of turtle graphics provide a dynamic and engaging learning experience. This makes it easier to understand the impact of your code and to debug errors.
- Creative Exploration: Logo empowers users to create their own drawings, games, and even simple animations, fostering creativity and encouraging experimentation.
- Understanding Fundamental Programming Concepts: Logo introduces core programming principles in a digestible way, building a solid foundation for understanding more complex programming paradigms later on.
Choosing a Logo Interpreter #
Several Logo interpreters are available, offering varying features and interfaces. When choosing an interpreter, consider the following:
- Platform Compatibility: Ensure the interpreter is compatible with your operating system (Windows, macOS, Linux).
- Ease of Use: Look for an interpreter with a user-friendly interface, clear documentation, and helpful online resources.
- Features: Some interpreters offer advanced features like 3D graphics or extensions for specific applications. For beginners, a basic interpreter with turtle graphics is sufficient.
- Availability: Choose an interpreter that is readily available for download and easy to install.
Popular options include (but are not limited to) FMSLogo, UCBLogo, and various online Logo interpreters. Research different options to find one that best suits your needs and preferences.
Setting up your Logo Environment #
The setup process will vary slightly depending on the chosen interpreter. Generally, it involves:
- Downloading and Installation: Download the chosen Logo interpreter from its official website or a trusted repository. Follow the installation instructions provided.
- Running the Interpreter: Once installed, locate and run the interpreter executable. This will usually open a text-based window or a graphical user interface (GUI).
- Familiarizing Yourself with the Interface: Explore the interpreter’s interface to understand how to enter commands, save programs, and access help resources. Many interpreters have a command line interface where you type commands directly. Others might have a GUI with menus and buttons.
- Testing a Basic Command: Try a simple command like
FORWARD 100
(or an equivalent) to ensure the interpreter is working correctly and the turtle graphics are functional. This command should move the turtle forward 100 units. Consult your interpreter’s documentation for specific commands and syntax. - Accessing Documentation and Tutorials: Familiarize yourself with the interpreter’s documentation and online tutorials. These resources will provide valuable information on using the interpreter’s features and functions.
Basic Commands and Concepts #
The Turtle Graphics Model #
Logo’s turtle graphics model is central to its appeal. Imagine a small turtle positioned at a specific point on the screen, facing a particular direction. This turtle can move forward, backward, turn right or left, and draw lines as it moves. The turtle’s position and orientation determine what is drawn on the screen. The commands you write control the turtle’s actions, effectively creating drawings and patterns. The screen itself acts as the canvas upon which the turtle creates its artwork. Think of the turtle’s pen as being either “down” (drawing as it moves) or “up” (moving without drawing).
Basic Movement Commands (FORWARD, BACK, RIGHT, LEFT) #
These four commands are fundamental to controlling the turtle’s movement:
- FORWARD distance: Moves the turtle forward the specified distance units. For example,
FORWARD 100
moves the turtle 100 units in the direction it’s currently facing. - BACK distance: Moves the turtle backward the specified distance units.
BACK 50
moves the turtle 50 units in the opposite direction of its current facing. - RIGHT angle: Turns the turtle right by the specified angle degrees.
RIGHT 90
rotates the turtle 90 degrees clockwise. - LEFT angle: Turns the turtle left by the specified angle degrees.
LEFT 45
rotates the turtle 45 degrees counter-clockwise.
The units of distance are usually pixels, but this might vary depending on your Logo interpreter’s configuration. The angles are always measured in degrees.
Understanding Angles and Turns #
Angles in Logo are measured in degrees. A full circle is 360 degrees. Turning right by 90 degrees means a quarter turn to the right, while turning left by 180 degrees means a half-turn. Understanding how angles affect the turtle’s orientation is crucial for creating complex shapes and patterns. It’s helpful to visualize the turtle’s movements and turns to predict the outcome of your commands.
Pen Control (PENDOWN, PENUP, PENCOLOR) #
These commands control the turtle’s pen and its drawing capabilities:
- PENDOWN: This command puts the turtle’s pen down. The turtle will now draw a line as it moves.
- PENUP: This command lifts the turtle’s pen up. The turtle can now move without drawing. This is useful for repositioning the turtle without leaving a trace.
- PENCOLOR color: This command sets the color of the turtle’s pen. The color argument can be a string representing a color (e.g., “RED”, “BLUE”, “GREEN”), or it could be a numeric code depending on your Logo interpreter. Check your interpreter’s documentation for the available color options.
Clearing the Screen (CLEARSCREEN) #
The CLEARSCREEN
command clears the drawing area, resetting the turtle to its initial position (usually the center of the screen) and pointing it in its default direction (usually upwards). This is useful for starting a new drawing or removing previous drawings.
Repeating Commands (REPEAT) #
The REPEAT
command is a powerful tool for creating repetitive patterns and shapes. Its syntax is typically:
REPEAT *number* [*commands*]
- number: Specifies the number of times the commands within the square brackets should be repeated.
- [commands]: A sequence of Logo commands enclosed in square brackets that will be executed repeatedly.
For example: REPEAT 4 [FORWARD 100 RIGHT 90]
will draw a square because it repeats the instructions to move forward 100 units and turn right 90 degrees four times. This demonstrates how REPEAT
simplifies drawing complex shapes with minimal code.
Creating Shapes and Designs #
Drawing Squares and Rectangles #
Squares and rectangles are easily created using the FORWARD
, RIGHT
, and REPEAT
commands. A square can be drawn with:
REPEAT 4 [FORWARD 100 RIGHT 90]
This code repeats the forward movement and right turn four times, creating four sides of equal length and 90-degree angles. To draw a rectangle, simply change the lengths of the sides:
REPEAT 2 [FORWARD 150 RIGHT 90 FORWARD 80 RIGHT 90]
This creates a rectangle with sides of length 150 and 80. Remember to adjust the FORWARD
distances to control the rectangle’s dimensions.
Drawing Triangles and Other Polygons #
Triangles and other polygons can be drawn by adjusting the number of sides and the angle of turn within the REPEAT
command. An equilateral triangle (all sides equal) can be created with:
REPEAT 3 [FORWARD 100 RIGHT 120]
The angle of 120 degrees is calculated as (360/3) = 120 degrees (360 degrees divided by the number of sides). For a regular pentagon (5 sides), use:
REPEAT 5 [FORWARD 100 RIGHT 72]
(360/5 = 72 degrees). This pattern can be extended to create any regular polygon by changing the number of sides and calculating the corresponding angle.
Creating Circles and Arcs #
Precise circles are difficult to draw directly in Logo using only FORWARD
and RIGHT
, as the turtle moves in discrete steps. However, we can approximate a circle using a large number of small sides. For example:
REPEAT 360 [FORWARD 1 RIGHT 1]
This code approximates a circle by moving forward 1 unit and turning 1 degree, 360 times. The smaller the FORWARD
distance and the RIGHT
angle, the smoother the circle will appear. Arcs can be created by reducing the number of repetitions in this pattern. For example:
REPEAT 90 [FORWARD 1 RIGHT 1]
This draws a quarter circle.
Combining Shapes to Create Complex Designs #
By combining the commands learned so far, you can create increasingly complex designs. For example, you could create a house by drawing a square (for the base) and a triangle (for the roof) consecutively. More elaborate patterns can be created using nested REPEAT
commands or by introducing procedures (as described in later sections). Experiment with different combinations of shapes and movements to build your designs.
Using Variables to Create More Dynamic Shapes #
Variables allow you to make your shapes more flexible and dynamic. Instead of hardcoding values like FORWARD 100
, you can use variables to store the side lengths. For example:
MAKE "side 50
REPEAT 4 [FORWARD :side RIGHT 90]
This code defines a variable side
with the value 50 and then uses this variable within the REPEAT
loop. By changing the value assigned to side
, you can easily change the size of the square without modifying the main REPEAT
structure. This makes your code more reusable and adaptable for creating various shapes and sizes. You can also use variables to change the angle of the turns or the number of repetitions.
Procedures and Functions #
Defining Procedures #
Procedures in Logo are blocks of reusable code. They allow you to group a sequence of commands together and give them a name. This makes your programs more organized, readable, and easier to maintain. A procedure is defined using the TO
and END
keywords. The general syntax is:
TO <procedure_name>
<command1>
<command2>
…
<commandN>
END
<procedure_name>
is the name you choose for your procedure (e.g., SQUARE
, TRIANGLE
, DRAWHOUSE
). The commands between TO
and END
are the actions the procedure will perform. For example, a procedure to draw a square could be defined as:
TO SQUARE
REPEAT 4 [FORWARD 100 RIGHT 90]
END
Calling Procedures #
Once a procedure is defined, you can “call” it by simply typing its name. When you call a procedure, the Logo interpreter executes the commands within its definition. For example, after defining the SQUARE
procedure above, you can draw a square by simply typing:
SQUARE
This will execute the REPEAT
loop and draw the square.
Using Input Parameters in Procedures #
Procedures become more powerful when they accept input parameters. Parameters allow you to pass values to the procedure, making it more versatile. For example, we can modify the SQUARE
procedure to accept the side length as a parameter:
TO SQUARE :SIDE
REPEAT 4 [FORWARD :SIDE RIGHT 90]
END
The :SIDE
indicates that the procedure accepts an input parameter named SIDE
. Now you can draw squares of different sizes:
SQUARE 50 ; Draws a square with side length 50
SQUARE 100 ; Draws a square with side length 100
The :
before the parameter name is crucial; it signifies that SIDE
is a parameter. Inside the procedure, :SIDE
accesses the value of the input parameter.
Creating Reusable Code Blocks #
Procedures are the cornerstone of creating reusable code blocks in Logo. Instead of repeatedly writing the same sequence of commands, you can define them as a procedure and call it whenever needed. This reduces redundancy, makes your code more concise, and simplifies debugging. Consider complex designs – procedures allow you to break down the design into smaller, manageable parts, each handled by a separate procedure. These procedures can then be combined to create the complete design.
Understanding Local and Global Variables #
- Global Variables: Variables defined outside any procedure are global variables. They are accessible from anywhere in your program, including inside procedures.
- Local Variables: Variables defined inside a procedure are local variables. They are only accessible within that specific procedure. Once the procedure finishes executing, the local variables are no longer available.
Using local variables is generally good practice. It prevents unintended modifications of variables from different parts of the program, improving code clarity and preventing bugs. For example:
MAKE "SIZE 100 ; Global variable
TO BIGGER_SQUARE
MAKE "SIZE 50 ; Local variable, only accessible inside this procedure
REPEAT 4 [FORWARD :SIZE RIGHT 90]
END
BIGGER_SQUARE ; Draws a square with side 50
PRINT :SIZE ; Prints 100 (the global variable is unchanged)
In this example, the local variable SIZE
within BIGGER_SQUARE
does not affect the global SIZE
variable. This separation is crucial for well-structured programs.
Advanced Concepts #
Working with Lists #
Lists are ordered collections of items in Logo. They are enclosed in square brackets []
and elements are separated by spaces. For example, [1 2 3 4]
is a list containing the numbers 1 through 4. Logo provides commands to manipulate lists, such as:
- FIRST: Returns the first element of a list.
FIRST [1 2 3]
returns1
. - LAST: Returns the last element of a list.
LAST [1 2 3]
returns3
. - BUTFIRST: Returns the list without the first element.
BUTFIRST [1 2 3]
returns[2 3]
. - BUTLAST: Returns the list without the last element.
BUTLAST [1 2 3]
returns[1 2]
. - FPUT: Adds an element to the beginning of a list.
FPUT 0 [1 2 3]
returns[0 1 2 3]
. - LPUT: Adds an element to the end of a list.
LPUT 4 [1 2 3]
returns[1 2 3 4]
. - ITEM : Returns the element at the specified index in the list.
ITEM 2 [1 2 3]
returns2
.
Lists are valuable for storing and processing collections of data, enabling more complex programs. They are particularly useful in conjunction with recursive procedures.
Using Recursion #
Recursion is a powerful programming technique where a procedure calls itself. It’s often used to solve problems that can be broken down into smaller, self-similar subproblems. A recursive procedure needs two key parts:
- Base Case: A condition that stops the recursion. Without a base case, the procedure would call itself infinitely, leading to a program crash.
- Recursive Step: The procedure calls itself with a modified input, moving closer to the base case.
A classic example is drawing a fractal pattern like a Koch snowflake:
TO K :LEVEL :SIZE
IF :LEVEL = 0 [FORWARD :SIZE STOP]
K :LEVEL - 1 :SIZE / 3
LEFT 60
K :LEVEL - 1 :SIZE / 3
RIGHT 120
K :LEVEL - 1 :SIZE / 3
LEFT 60
K :LEVEL - 1 :SIZE / 3
END
This procedure recursively draws smaller and smaller triangles until the base case (LEVEL = 0
) is reached.
Creating Interactive Programs #
Interactive programs allow the user to input data and affect the program’s flow. Logo supports this through commands that obtain input from the user:
- READLIST: Reads a list of words from the user’s input.
- READWORD: Reads a single word from the user’s input.
- PRINT: Displays output to the user.
Combining these with conditional statements (IF
, IFELSE
) allows for branching logic based on user input, creating dynamic and engaging applications. For example, a program that draws a shape based on user input could be created using these commands.
Working with External Files #
More advanced Logo implementations may allow interaction with external files. This enables saving and loading program data, configurations, or even user-created drawings. The exact commands will depend on the specific Logo interpreter. Common operations include:
- Opening a file: Preparing a file for reading or writing.
- Reading from a file: Retrieving data from a file.
- Writing to a file: Saving data to a file.
- Closing a file: Releasing the file resource.
The ability to work with external files significantly expands the capabilities of your Logo programs, allowing for persistent storage and data management beyond the immediate session. Consult your Logo interpreter’s documentation for the specific commands and syntax related to file I/O.
Example Projects #
Drawing a Simple House #
This project demonstrates combining basic shapes to create a more complex image. We’ll use procedures to modularize the code and improve readability.
TO SQUARE :SIZE
REPEAT 4 [FORWARD :SIZE RIGHT 90]
END
TO TRIANGLE :SIZE
REPEAT 3 [FORWARD :SIZE RIGHT 120]
END
TO HOUSE :SIZE
SQUARE :SIZE
RIGHT 30
FORWARD :SIZE
LEFT 150
TRIANGLE :SIZE
END
HOUSE 50
This code defines procedures for drawing a square and a triangle. The HOUSE
procedure then uses these to draw a simple house shape. The HOUSE
procedure takes a size parameter to control the overall dimensions of the house.
Creating a Star Pattern #
This project uses repetition and angles to create a five-pointed star. We’ll employ the REPEAT
command for efficiency.
TO STAR :SIZE
REPEAT 5 [FORWARD :SIZE RIGHT 144]
END
STAR 100
The STAR
procedure draws a single star. The angle 144 is calculated as (180 - 360/5) = 144. This ensures the points of the star are equally spaced. Experiment with different SIZE
values and even add more nested REPEAT
commands for more complex star patterns.
Designing a Simple Game #
This example outlines a rudimentary guessing game. This showcases user interaction using READWORD
and conditional statements. This is a simplified version; a more robust game would require more advanced techniques, error handling, and potentially the use of lists for storing game data.
TO GUESSING_GAME
PRINT [Guess a number between 1 and 10:]
MAKE "SECRET 5 ; The secret number
MAKE "GUESS READWORD
IF :GUESS = :SECRET [PRINT [You guessed correctly!] STOP]
IF :GUESS < :SECRET [PRINT [Too low!]]
IF :GUESS > :SECRET [PRINT [Too high!]]
END
GUESSING_GAME
This game generates a secret number (5 in this example) and prompts the user for a guess. It then provides feedback based on the user’s input. To enhance this game, you could add features like multiple guesses, range selection, and scorekeeping. These would involve further use of loops and variables. Remember to adapt these examples to your specific Logo interpreter, as commands and syntax might vary slightly.