YoYo Tutorial

Created 1/27/97 -- Modified 12/9/98
YoYo is a programming language built on top of Java. YoYo is descended from Logo. It is object oriented and integrates well within the Java language framework.

This document is intended to be an introduction to YoYo from the ground up. It will introduce the syntax, semantics and basic elements of YoYo, hopefully in a way that a person may "get up" on YoYo in just a short time.


Start

Well, since we have to begin somewhere, let's start by explaining what you just downloaded. After you unpacked YoYo you got a yoyo.jar file, several .yoyo files, and several .html files. There are also batch files that let you easily run YoYo from the command line. The YoYo files are used by YoYo when running your YoYo code.

Before you begin to use the YoYo development environment, you need to have a copy of Java. You can either use a supported browser, or go to Sun's web site (http://java.sun.com) and download the JDK 1.1.x. You may also download the JRE 1.1.x if you do not need to program in Java.

To start the interactive interpreter, run the class yoyo.Toplevel (methods will be differnet depending on your particular Java platform. On Macs, there will be a YoYo CommandCenter application. Double-click it. On PCs, you can double-click the yoyo.html file to run the command center in IE 4.0, or use the command-line java runner (java, jview, or jre, depending on which Java you're using) to run the class yoyo.Toplevel).

This will bring up a text window with the interpreter in it. Upon startup, it says "Welcome to YoYo!" and blinks a prompt at you.

Once YoYo has started, you may type a command in the text box and press return to run it.

print 5 + 6
11

If you say print 5 + 6, YoYo computes 11 and prints it to the interpreter and says ok. Print is a command that takes one input and prints the value in the interpreter window. + is an infix operator that takes in two numbers and returns the addition of them.

If you ever want to execute print 5 + 6 again, you don't need to retype it. Just move the cursor to anywhere on that line and press return. YoYo will run that line again.


Intro to Math

YoYo supports lots of different math functions with order of operations. Let's try some now:

print 5 + 6
11

Notice the dot that flashes quickly at the end of the 6? That dot tells you that YoYo is computing something. It goes away when you're allowed to type again. If you hit a key while YoYo is computing, it will cancel the operation.

print 5 - 6
-1

print 5 * 6
30

Notice that there is a space between every word in YoYo. This is important.

print 6 / 5
1.2

Also notice that YoYo supports real numbers as well as integers. In fact, all numbers are real (regardless of how they print out) unless it is necessary to coerce them to integers for some internal calculation.

print 6 mod 5
1
print 6 / 5 + 3
4.2

print 6 + 5 / 3
7.6666

So far, you've only seen infix math operators. YoYo also has prefix operators which act like functions.

print min 5 6
5

print random 6
3

print random 6
5

Random is a function that takes a number and generates a random integer between 0 and the number. (0 <= random < x)

print random 5 + 7
9

When a prefix function takes an argument, you may supply the result of another function. 5 + 7 returns 12 to random, which takes this and uses it make a new random number between 0 and 12.

If you're unsure about how YoYo is reading your prefix and infix calculations, you may put parentheses around each part to force YoYo to read things your way.

print (random 5 + 7) * 6
18

YoYo picked 3 to be a random number between 0 and 12 and then multiplied it by 6 to get 18.

There are other math functions, which are listed in the reference manual.


Intro to Lists

YoYo is a dialect of Logo, which is a dialect of Lisp. Its basic data struction is a list. A list is a group of items that may contain anything including other lists. Lists are mutable and may be added to in the front or on the back.

To make a list, do this:

print list "a" 6
[a 6]

We just made a list with two elements, the string "a", and the number 6. List is a function that takes two inputs and creates a list with those two elements. You might notice that the quotes are gone when the list was printed out. When strings are printed out, the quotes are left off because only words can be strings.

Lists are also printed with two []'s at either end. You may type a list in directly with these braces to avoid using a function like list.

print [a 6]
[a 6]

print [a 6 4 [c d e 5] b]
[a 6 4 [c d e 5] b]

You might notice that it's also more convenient when you want to create a list with many elements in it.

If you want to add elements to a list or join two lists there are functions to do that.

print fput "a" [b c d]
[a b c d]

print lput "b" [z y x]
[z y x b]

print sentence [a b] [c d]
[a b c d]

print se "a" [c d]
[a c d]

Se is an abbreviation for sentence. Sentence takes two inputs, a string or a list and joins them together. (like append in Lisp, except sentence will listify any string into a list before joining them)

Lists also come with functions to get values out of them.

print first [a b c]
a

print last [a b c]
c

print item 2 [a b c]
b

First and Last are self-explanatory. Item takes in two inputs: an index number and a list. The indexth element of list is pulled out and returned. (item counts from 1)

You may also do other operations on lists.

print length [a b c d e f [g h i]]
7

print list? [a b c d]
true

True and false are the booleans in YoYo. Only false is false, everything else is true. (3 is true, 0 is true, "foo" is true, true is true, not false is true; false is false, not 3 is false, not [] is false, etc...)

print position "b" [a b c d]
1

print position "polly" [a b c d]
false

Position tells you the position of an element in the list. It starts at index 0 of the list. If it does not find it, position returns false.

Notice that all of these commands take an input list and return a new list as output. None of them mutate (change) the list. The only YoYo list mutator is set-item (which can alter an element of a list).


Intro to Variables

In YoYo, variables do not have to be declared and can contain anything. There are no static types in YoYo. (and no static type checking!)

To make a new variable do,

set foo list "a" 6

Now we have a new variable called foo that contains the value [a 6].

print foo
[a 6]

We can refer to the value of foo by just using its name.

set bar list "b" 5

set cow se foo bar

print cow
[a 6 b 5]

Even though foo contains [a 6] now, you can change it to be anything later.

set foo 6

print 5 * foo
30

set foo [polly wants a cracker]

print item 3 foo
a


Intro to Functions

YoYo also has procedures. There are two ways to define procedures. The simple way is to create a new text file with any text editor. In the file, type in

to my-new-function
print 5 + 6
set foo 3
print foo * 9
end

and then save it as test.yoyo. Go back to the YoYo interpreter and type in

load "test"

You don't need the add the .yoyo suffix. YoYo assumes it needs to attach it for you. Test.yoyo is loaded into the current interpreter and you can now use your new function by typing its name.

my-new-function
11
27

You can also create functions that take inputs. These inputs are treated as local variables and may only be used within the function.

to my-next-function a b c
print a * b
print first c
set a (first c) * a
print a
end

This is probably a good time to talk about commands and reporters. A command is a function that doesn't return any value (void for you C people). Some examples are "print" and "set". A reporter is a function or data value that returns a value. 6 is a reporter, as well as "list 1 2".

Now continuing with our example:

load "test"

my-next-function 5 6 [7]
30
7
35

print a
a is not defined

Procedures also may output values and become reporters.

to my-third-function a b c
print a * b
output 0 - b + a * a - first c
end

set foo my-third-function 5 6 [7]
30

print foo
12


Intro to Control Structures

There are several different ways to make your program data dependent. The first is the if statement.

if 5 < 6 [print "math is king!"]
math is king!

if 5 = 8 [print "math is infallible"]

If takes a boolean (or expression that evaluates to a boolean) and a list. If the boolean is not false, it will run the list.

ifelse 6 < 7 [print "i smell."] [print "you smell."]
i smell.

Ifelse is a variant of if that can run either the consequent or alternative if the predicate is true or false.

Repeat is another control structure that you may recognize from Turtle Logo.

repeat 5 [print "hello"]
hello
hello
hello
hello
hello

It takes a number and a list and runs the list n times.

set i 0

while [i < 5] [print i set i i + 1]
0
1
2
3
4

While takes two lists. The first is the predicate which is checked at the beginning of every loop. The second is the list to run when the predicate is true.

If you make a mistake:

set i 6
while [i > 5] [print i set i i + 1]
6
7
8
9
10
.
.
.

and the program is not stopping, press any key and it will break.

56666
56667

Cancelled!


Intro to Objects

Note: Objects are an advanced topic and not suited for beginner programmers. YoYo's easier data structures are perfectly sufficent for kids in elementary school.

Since Java is an object oriented language, we felt that YoYo should be one too. Besides, it makes it easier to run Java code in your YoYo programs.

Unlike Java, YoYo uses a prototype-based inheritance model. All objects are instances. There are no classes. When you want to subclass an object, find a good one laying around and copy it and then customize the copy.

There are only a few built in object commands in YoYo, all the rest are specific to a particular object.

set foo new-object

print foo
<Object #45: Object>

foo is now a new object with nothing in it. It has type "Object." To add a field to foo,

set foo.a "bar"

print foo.a
bar

We just added the "a" field to foo. The foo.a syntax is reminiscent of Java. Let's add some more.

set foo.b [a 5 3]

set foo.c to-list [foo.a foo.b]

foo.b is now set to the list [a 5 3]. foo.c is now set to the list [bar [a 5 3]]. To-list is a procedure that takes an instruction list and evaluates everything inside. It then takes all the results and makes them into a list. It's the functional analog to [foo.a foo.b] which would interpret foo.a and foo.b as strings and not as variables.

Now, you might get the desire to have a copy of this foo object.

set bar copy-object foo

print bar
<Object #46: Object>

Now bar is a complete copy of foo. (copy-object copies the pointers to the values inside, it doesn't copy the values.)

We can then modify bar and foo will be untouched.

set bar.a [big man]

print bar.a
[big man]

print foo.a
bar

You can consider copying an object to be the same as subclassing it. Copies of objects come with a built-in field called parent.

print bar.parent
<Object #45: Object>

print foo.parent
false

Foo doesn't have a parent since it was a new object.


This is the end of this simple introduction to YoYo. I'll be making more documentation soon, but, until then, try to learn by reading other pieces of YoYo code for inspiration.


Andrew Begel
Epistemology and Learning
MIT Media Lab