Home Up Contents Search

Logo

Commands
Exercise 1
Exercise 2
Exercise 3

The Logo Programming Language

The Algorithms and Programming topic will be introduced using MSW Logo to help you recognise the basic control structures and elements of algorithms. The Logo computer language is ideal for beginning programmers because it provides immediate visual responses to coding. The Logo computer language will be revisited later when it is used to control robot arms and other devices in the Artificial Intelligence unit.

MSWLogo

Copyright (C) 1989 The Regents of the University of California. This Software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies.

Copyright (C) 1993-1997 George Mills. This Software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies.

Why Logo?

This introduction does not do LOGO justice but it's a start. LOGO is a programming language, pure and simple. There are two models that languages come in, compiled and interpreted.

What is a compiled language? In a compiled language the program is written and fed to a compiler. A compiler reads all your code and converts it to an executable form that your computer understands.

What is an interpreted language? An interpreted language does not get compiled, instead, as each line is read by the interpreter and executed. This is a slow process to execute (on the fly) like this, but has the advantage of not requiring a complete compile for each change. It's ideal in a learning environment.

So have guessed what type of language LOGO is yet? Right, it's an interpreted language; at least this LOGO is anyway.Turtle

LOGO also has another unique feature not offered in many other languages (none that I know of). That is, what's called "Turtle Graphics".

What are turtle graphics? Turtle graphics is a simple and powerful set of commands to manipulate a turtle.

Why do they call it a turtle? The first version of LOGO used an electronic robot that resembled a turtle. In the case of a video screen (like this LOGO) it's simply a cursor (or pointer) of where the turtle is.

What does the turtle do? It draws, lines mostly, on the screen.

The gap that turtle graphics fills is what traditional languages do not. That is, it gives immediate feedback. Immediate feedback makes it fun and easier to learn programming. The purpose of LOGO is to teach young and old how to program. It was modelled after a very popular and power language called LISP. It is as powerful as any other programming language.

Where to Start

Novices can start in LOGO without having to program at all by just learning how to command the turtle. Learning turtle graphics will teach the user about geometry (and they won't even know it). It's amazing how soon you can introduce the concept of programming once they grasp the turtle concept. Lets look at some simple examples:

Draw a square using the turtle

FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90

That was easy but too much typing, let's try again.

REPEAT 4 [FD 100 RT 90]

That's it? Yes, that's the same square. We did two things. We noticed too much redundant code in our first example, so we asked logo to repeat the same sequence 4 times. We also used abbreviated forms of the same commands. But we can still do better. A square is a popular item wouldn't you say? Wouldn't it be more useful just to say square when you wanted a square?

EDIT "square

<Editor will pop up>

TO SQUARE
  REPEAT 4 [FD 100 RT 90]
END

<Exit Editor and save>

SQUARE
SQUARE

What's the TO and END for? It's to define a procedure (a small program) for the square. The TO can be thought of as "to do something", the END terminates the TO. Once square was "defined" we then called it twice. That's all you need to get a square now, just type square. There is a problem, however. It only draws squares of 100 by 100. Wouldn't it be better to draw any size square? It sure would and it's easy.

EDIT "square

TO SQUARE :length
  REPEAT 4 [FD :length RT 90]
END

SQUARE 100
SQUARE 200

Note all we did is replace 100 with a variable name called :length. Now when we call square we must specify how big we want it. Above we asked logo to draw one square at 100x100 and another at 200x200. Note the ":" in front of the word length tells logo that length is a variable. However, we can still even do better. What's wrong now, you ask. Well, wouldn't it be better if we could draw something other than a square like a triangle?

TO TRIANGLE :length
  REPEAT 3 [FD :length RT 120]
END

TO SQUARE :length
  REPEAT 4 [FD :length RT 90]
END

TO PENTAGON :length
  REPEAT 5 [FD :length RT 72]
END

TRIANGLE 100

SQUARE 100

HEXAGON 100

Lot of typing (programmers hate to type). Why? Because there are more things to break and when a change needs to be made it might have to be made in many places. Smaller is not always better but it usually helps. Let’s try again.

TO POLYGON :length :sides
  REPEAT :sides [FD :length RT 360.0/:sides]
END

POLYGON 100 3

POLYGON 100 4

POLYGON 100 5

What happened to TRIANGLE, SQUARE and HEXAGON? POLYGON now acts as every equal-sided polygon possible and with only one line of code! We now repeat the sequence based on how many :sides the caller asked for and we turn (RT) the amount of degrees appropriate for that shape. You may not realise it but this is PROGRAMMING.

Now that we have a program its a good idea to save it to the disc. The edits you’ve made so far are all within Logos memory and not on the disc. How do you save your work, easy.

SAVE "SHAPES.LGO

BYE

If you ever want to use these definitions again you’ll have to reload them. How do you reload you work from disc?

LOAD "SHAPES.LGO

I could go on forever, yes forever, even to the point of writing LOGO within LOGO. This should get you started.

 
Next
Send mail to ljschwerin@hotmail.com with questions or comments about this web site.

Copyright © 1999-2002 Leon Schwerin
Last modified: 28 March 2000
1