Day 2 made some huge leaps and bounds over the basic syntax introduced in
Day 1. The key learning from this day is that in Io, just about everything is
a message sent to an object. There aren’t separate semantics for calling
functions, using control structures, or defining objects: those are all just
objects reacting to some sort of message.
One of the most startling examples of this is the realization that even
the basic operators in Io, such as +, -, and *, are actually messages. That
is, the code 2 + 5 is actually understood as the message + being sent to
the object 2 with 5 as a parameter. In other words, it could be re-written as
2 +(5). The +, then, is just a method defined on the number object that
takes another number as a parameter.
This makes supporting operators on custom objects simple: all I have to
do is define a “slot” with the operator’s name. For example, here’s an object
for complex numbers that can be used with the + operator:
I found this fairly eye opening. As I think of the syntaxes of other
languages I’m used to, such as Java, there are “special cases” all over the
place. For example, the + operator has special code to handle addition for
numbers and String concatenation and nothing else; for loops, while loops, if
statements, defining classes, and so on are all special syntax features. In
Io, they are all just objects responding to messages.
Io, Day 2: Problems
Write a program to find the nth Fibonacci
number. Both the recursive and
iterative solutions are included:
How would you change the / operator to return 0 if the denominator is zero?
Write a program to add up all the values in a 2-dimensional array.
Add a slot called myAverage to a list that computes the average of all the
numbers in a list. Bonus: raise an exception if any item in the list is not a
Two Dimensional List
Write a prototype for a two-dimensional list. The dim(x, y) method should
allocate a list of y lists that are x elements long. set(x, y, value) should
set a value and get(x, y) should return that value. Write a transpose method
so that new_matrix get(y, x) == original_matrix get(x, y). Write the matrix to
a file and read the matrix from a file.
Write a program that gives you ten tries to guess a random number from 1-100.
Give a hint of “hotter” or “colder” for each guess after the first one.