Seven Languages: Week 2 (Io) - Day 1
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”.
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.
- #io on freenode
Strongly typed. Trying to run
1 + "one" throws an exception that says:
"Exception: argument 0 to method '+' must be a Number, not a 'Sequence'"
if(0) println if("") println if(nil) println
true true false
# <prototype> slotNames Zerg := Object clone Zerg sixPool := "Zergling rush!" Zerg slotNames println
- = 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
From the command line run: io day1.io
# 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! Done!
Next in this series: Day 2 of Io