2006-10-27

The Icon programming language

While I was at OOPSLA I decided to learn the Icon programming language. I read The Icon Programming Language, Third Edition by printing out a copy at Kinkos.

The main reasons I decided to learn Icon as my next language is that one of the creators recently died, Python copied generators from it, and Uncle Timmy likes it. I personally like knowing the original context of where the newer features of Python came from (one of the reasons I learned Haskell).

It's a rather funky language. First, everything is an expression. Next, you need to realize that it is like Lua and JavaScript where almost everything has a default value so that generating errors is hard (but if you mess up it is difficult to notice). These two things lead to Icon's idea of success and failure; expressions don't error out, they just either succeed and thus occur, or they fail and they halt. If you have a multi-part expression and any part of it fails, then execution of that expression halts and Icon moves on to the next expression.

An example would probably help. Let's say you had the statements ``write(a_list[2]); write(a_list[1])``. Now, if you passed in a list with at least two items (Icon starts indexing at 1), it would print out the first two items on their own lines. But if a_list only had one item, the first expression would fail, but the second expression would succeed! It leads to writing code where you say "try to do this, but if you can't, then oh well". It also continually tries to have an expression succeed, attempting to exhaust combinations of arguments looking for a succeeding solution.

The generator support is pretty extensive. '!' basically creates an iterator for a container object. '|' creates a chaining generator. Ties this into expressions that continually execute generators within the expression passed in and you end up with cool stuff like ``every write(1 to 10)`` that prints every number from 1 to 10 on its own line.

It also has extensive string manipulation support (thanks to it's creator also having worked on SNOBOL). Ties this in with generators and succeed/failure ideas and you end up with a very nice way of writing succinct string code.

It's not going to supplant Python as my favourite language any time soon, but it was interesting to learn. I have to admit I was able to pound out my four simple little apps that I write all new languages I learn rather quickly and in a slightly simpler manner than normal for some of them.