11.03.2015 Views

Unicon Book

Unicon Book

Unicon Book

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2<br />

Structures<br />

The examples in the previous chapter employed data types whose values are immutable.<br />

For example, all operations that manipulate numbers and strings compute new values,<br />

rather than modify existing values. This chapter presents structured types that organize<br />

and store collections of arbitrary (and possibly mixed) types of values. When you complete<br />

this chapter, you will understand how to use these types.<br />

• Tables associate their elements with key values for rapid lookup.<br />

• Lists offer efficient access by position as well as by stack or queue operations.<br />

• Records store values using a fixed number of named fields.<br />

• Sets support operations such as union and intersection on groups of elements.<br />

• Using structures to represent trees, graphs, and matrices.<br />

There are several structure types that describe different basic relationships between values.<br />

The philosophy of structures in <strong>Unicon</strong> is to provide built-in operators and functions for<br />

common organization and access patterns - the flexible ”super glue”that is needed by nearly<br />

all applications. Their functionality is similar to the C++ Standard Template Library or<br />

generic classes in other languages, but <strong>Unicon</strong>’s structure types are much simpler to learn<br />

and use, and are well supported by the expression evaluation mechanism described in the<br />

previous chapter.<br />

All structure types in Icon share many aspects in common, such as the fact that structures<br />

are mutable. The values inside them may change. In that respect, structures are<br />

similar to a collection of variables that are bundled together. In many cases, <strong>Unicon</strong>’s<br />

structure types are almost interchangeable! Operators like subscripts and built-in functions<br />

such as insert() are defined consistently for many types. Code that relies on such operators<br />

is polymorphic: it may be used with multiple structure types in an interchangeable way.<br />

For both the structures described in this chapter and the strings described in the next<br />

chapter, be aware that <strong>Unicon</strong> performs automatic storage management, also known as<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!