Jump to content

Io Programming/Beginner's Guide/Basic Components of Io Programs

From Wikibooks, open books for an open world

Basic Components of Io Programs

[edit | edit source]

There are a number of basic components you need to be familiar with to make effective Io programs.

Keywords

[edit | edit source]

Strictly speaking, Io has no keywords. However, it does have a syntactic sugar layer that, for all intents and purposes, gives it a few keywords. There are only three of these keywords-that-aren't-really-keywords in the whole language. We've seen one already.

;
The semicolon is used to separate different things the computer should do. You won't be using this often, but it sometimes can come in quite handy.
=
The equals is used to update an already existing variable. But, what if we don't already have a variable declared? Enter . . .
:=
This assignment operator (read, "becomes") is used to create a new variable (it also updates it if it happens to already exist).ยท

We won't get into how these not-exactly-keywords affect Io just yet. For now, let's just pretend that they're real keywords.

Strings

[edit | edit source]

Strings are strings of characters, really. Those are chunks of text wrapped inside quotation marks, like this:

Io> "Hello world!"
==> "Hello world!"

Sometimes, it is awfully convenient to put line breaks, tabs, or even bells (but not whistles) into text too. We do this with escape sequences. Far and above, the #1 escape sequence you'll use the most is \n. This particular escape sequence signals a newline, the equivalent of pressing enter. If you just pressed enter in the middle of this kind of string, it would confuse Io.

I mentioned "this kind" of string, because this ended up being a big enough problem that a new kind (borrowed from Python) was eventually implemented. This is the now famous triple-quoted string. They look like this:

Io> """Hello world!"""
==> "Hello world!"

What's the difference? The triple-quoted string does allow you to type the ENTER key right in the middle:

Io> """Dear sir,
"->
"-> I must admit, I really like your suggestion
"-> to learn Io.  Thank you for the kind words.
"->
"-> Sincerely,
"->
"->
"-> Robert.
"-> """
==> Dear sir,

I must admit, I really like your suggestion
to learn Io.  Thank you for the kind words.

Sincerely,


Robert.

Return Values

[edit | edit source]

You wouldn't normally see the results that functions and procedures produce listed as a language element in most books. But, Io is different -- it is a very real language element that you must deal with. This is pretty ominous, isn't it?

In Io, as with a lot of functional programming languages, everything you say or do in Io is an expression. This is "fancy computer science talk" for, everything you say or do produces some kind of result which can be used by some subsequent part of a program. Now you know why we prefer the computer science way over the plain-English way of saying it!

Let's break this down a bit further. An expression is anything that produces a value of some kind. A value can be a string, a number, a boolean (true/false) thingy, whatever. Examples:

Io> 4+4
==> 8
Io> 15 - 150
==> -135
Io> "Hi!"
==> Hi!

OK, so we just demonstrated that Io is a glorified calculator. In and of itself, this is entirely uninteresting. But, what makes an expression useful is the fact that we can do stuff with the results before actually giving the final result.

Io> "There are " .. (8*10)+19 .. " bottles of beer on that very wall, over there."
==> There are 99 bottles of beer on that very wall, over there.

Here, we use the .. operator to concatenate things together into a single value. In this case, we're tacking the result of (8*10)+19 onto the string "There are ", giving us "There are 99". Then, " bottles of beer...." is appended onto that, giving us the result we see above.

If Io has nothing else to do with the result of an expression, it prints the ==> arrow, and the result, with the assumption that you care about it. Usually, of course, you do, otherwise, you wouldn't have let the result go without further processing to begin with!

In the event that nothing useful is able to be returned from some computation or action, then nil is returned. As we saw earlier when playing around with writeln, we noticed that it returned the nil value. That's because it doesn't actually compute anything for us; it's a pure action.

Numbers

[edit | edit source]

Numbers are, pretty much, as you expect them to be. Integers and real numbers are both treated equally (in fact, currently, they're always converted into double-precision floating point notation for uniformity). Here are a few examples of numbers:

Io> 45
==> 45
Io> 4.5e+2
==> 450
Io> 4.5e+38
==> 4.500000e+38
Io> -15360.263823
==> -15360.263823

Hexadecimal values are also permitted using the C-inspired syntax 0x and the hex digits:

Io> 0xDEAD
==> 57005
Io> 0xBEEF
==> 48879
Io> 0xDEADBEEF
==> 3.735929e+09
Io> 0xDEADBEEFFEEDFACE
==> 4.294967e+09

That last number shows us something interesting -- it's clearly too small a result for a hexadecimal number so big! It turns out that, when converting hexadecimal numbers into its internal representation, only 32-bits worth of precision is maintained. If a hexadecimal number is larger than this, then the largest 32-bit integer is used instead.

Some might say this is a bug in Io. Perhaps; but it is not for this book to judge. However, if you intend on manipulating hexadecimal values with greater than 32-bits of precision, you'll need to convert them manually into decimal notation first.

Some other languages support concepts like complex numbers, infinite precision numbers, etc. Io doesn't support these with the basic set of primitives provided by the language's interpreter. However, these can be added either by external add-ons, or through Io itself.

Objects

[edit | edit source]

Surprise -- Io is an object oriented language, even though we never introduced them up until now.