« Back to posts

Seven Languages: Week 2 (Io) - Day 1

18 Dec 2011

Day 1 of Io is the first of many chapters that will give me that ‘new at programming’ feeling again, where I fumble just trying to build a valid expression that does what I want it to. I actually like that feeling; it means I am learning something new and a web of structures is forming in my mind. I like it almost as much as finally making that first deep connection from a group of new concepts to some of your existing ones, and starting to grasp the parallels. Fortunately for me, that is what this book is all about!

(This article is part of a series of posts I am doing about my journey through the exercises of the book Seven Languages In Seven Weeks. The article previous to this one is Week 1 (Ruby) - Day 3. For an overview see the Seven Languages project page.)

How Io feels

Syntactically, Io is a very very lightweight language, evocative of lisps in its regularity and minimalism. It provides a nice contrast with the previous chapter on Ruby. Ruby’s definition of simple code is something like “The more an expression reads like a (succinct) explanation of its intent, the better” whereas Io’s definition of simple code is something like “The less extraneous syntax that is needed to represent an expression, the better”.

Because of this approach, Io lends itself very well to metaprogramming – at least as well as Ruby does, possibly better. It does not have as the wide and varied set of built-in metaprogramming tools that Ruby does, but it also needs far fewer due to the way the language is constructed. Io is a prototype-based language, the first of the less familiar programming paradigms in this book. I won’t go into depth about this here except to note that Javascript is prototype-based as well, so learning Io could be very beneficial to anyone that does a fair amount of programming Javascript.

Highlights from exercises

The homework this week is short but well-constructed. It points out a couple of things that are important to remember for a newbie to Io. In particular, you will need slotNames a lot, so remember that one!

Other than that, there isn’t much to say about the homework this week. Remember slotNames and read the code listing.

Full Code Listing

Here is a nicely formatted version of my solutions to the exercises from Day 1 of Io. The home of the following code is on github with the other exercises.


Some Io example problems
An Io community that will answer questions
A style guide with Io idioms


1. Evaluate 1 + 1 and then 1 + "one". Is Io strongly typed or weakly typed?

Strongly typed. Trying to run 1 + "one" throws an exception that says: "Exception: argument 0 to method '+' must be a Number, not a 'Sequence'"

2. Is 0 true or false? What about the empty string? Is nil true or false?
if(0) println   
if("") println  
if(nil) println 
3. How can you tell what slots a prototype supports?
# <prototype> slotNames

Zerg := Object clone
Zerg sixPool := "Zergling rush!"
Zerg slotNames println 
list(type, sixPool)
4. What is the difference between = (equals), := (colon equals), and ::= (colon colon equals)? When would you use each one?
  • = is used to assign something to an existing slot
  • := is used to assign something to a previously non-existent slot
  • ::= is used to assign something to a previously non-existent slot as well as create a setter for that slot

iolanguage.com's guide puts this much much better than I did:

  • ::= Creates slot, creates setter, assigns value
  • := Creates slot, assigns value
  • = Assigns value to slot if it exists, otherwise raises exception


1. Run an Io program from a file.

From the command line run: io day1.io

2. Execute the code in a slot given its name.
# I'm not quite sure I understood this question. I'll answer the two
# interpretations I could come up with.

# If the code in a slot is stored as a method then just invoking the slot is
# enough:

Zerg macroItUp := method("Injecting larvae now!" println)
Zerg macroItUp # Will print "Injecting larvae now!"

# If the code in a slot is stored as a string then you should use something like
# doString:

Zerg macroHarderSteps := ("\"Spreading creep now!\" println")
Zerg macroHarder := method(doString(Zerg macroHarderSteps))
Zerg macroHarder

# Update: Re-reading this now the intent of the question seems obvious! Write a
# method that, given a method name, will try to execute that method.
"\nLet's try that again" println
Zerg specifyMacro := method(name, perform(name))
Zerg specifyMacro("macroItUp")
Zerg specifyMacro("macroHarder")
"Done!" println
Injecting larvae now!
Spreading creep now!

Let's try that again
Injecting larvae now!
Spreading creep now!

Next in this series: Day 2 of Io

blog comments powered by Disqus
Content by Nick Knowlson: Google+