A Glimpse of Icon

This article gives a quick introduction to the programming language Icon, developed at the University of Arizona.
Graphics and User Interfaces

Icon's built-in graphics have about 40 functions and introduce only one new type, the window, which is a special extension of the file type. This contrasts with graphics APIs in other languages where learning graphics means learning 400 or more functions that manipulate several dozen new types of values. Passing strings and integers into a few functions is all you need to write amazing graphics without excessive code.

One demonstration of Icon graphics is Brad Myers' “rectangle-follows-mouse” test, a program that opens up a window in which a rectangle follows a mouse around on the screen. A window is opened (file mode “g”) with an XOR raster drawing operation that causes graphics to erase themselves when redrawn. In the loop, for each user event, the ten-pixel square is erased and redrawn at the new mouse location. &x and &y are Icon keywords which hold the current mouse location and are saved in variables x and y. The variables x and y start out as null. The expression \x fails if x is null, causing the first call to DrawRectangle to be skipped the first time through the loop, since at this point, there is no rectangle to draw.

procedure main()
 w := open("win","g", "drawop=reverse")
 repeat {
 # get mouse/keyboard event
 Event(w)
 # erase old rectangle
 DrawRectangle(w, \x, y, 10, 10)
 # draw new rectangle
 DrawRectangle(w, x := &x, y := &y, 10, 10)
 }
end

Simple graphics programming is easy, but complex graphics are also possible. The Icon Program Library (IPL), a collection of Icon utilities and libraries, offers a more extensive Motif-style user interface toolkit as well as a WYSIWYG (what you see is what you get) interface builder that lets you build interfaces by drawing them. The IPL contains several other examples of graphical games and applications.

POSIX Made Simple

The Unicon flavor of Icon adds an elegant set of UNIX system-level facilities. An ultra-simple version of the ls utility illustrates some of these features. This version takes a directory name on the command line and produces a listing of file information including file size and modified time, sorted by name. (A more interesting version is included in Linux Gazette article.)

ls reads the directory and performs a stat call on each name it finds. In Icon, opening a directory is exactly the same as opening a file for reading; every read returns one file name.

$include "posix.icn"
procedure main(argv)
 f := open(argv[1]) |
 stop("ls: ", sys_errstr(&errno))
 names := list()
 while name := read(f) do
 push(names, name)
 every name := !sort(names) do {
 p := lstat(name)
 write(p.size, "   ", ctime(p.mtime)[5:17],
 " ", name)
 }
end

The lstat function returns a record with all the information that lstat(2) returns. In the Icon version, the mode field is given as a human readable string—not an integer to which you must apply bitwise magic. Also, in Icon, string manipulation is very natural.

Give Icon a try; whether you're a programmer or not, you'll love it.

Resources

Clint Jeffery is an assistant professor in the Division of Computer Science at the University of Texas at San Antonio. He writes and teaches about program monitoring and visualization, programming languages and software engineering. Contact him at jeffery@cs.utsa.edu or read about his research at www.cs.utsa.edu/faculty/jeffery.html. He received his Ph.D. from the University of Arizona.

Shamim Mohamed met UNIX in 1983 and was introduced to Linux at version 0.99 pl12. These days he is a Silicon Valley polymath and factotum, and an instrument-rated pilot flying taildraggers. He can be reached at spm@drones.com or www.drones.com/. He received his Ph.D. from the University of Arizona.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState