Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Icon (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Collections=== Icon includes several [[Collection (abstract data type)|collection types]] including [[List (abstract data type)|lists]] that can also be used as [[Stack (abstract data type)|stacks]] and [[Queue (abstract data type)|queues]], [[associative array|tables]] (also known as maps or dictionaries in other languages), [[Set (abstract data type)|sets]] and others. Icon refers to these as ''structures''. Collections are inherent generators and can be easily called using the bang syntax. For instance: <syntaxhighlight lang="icon"> lines := [] # create an empty list while line := read() do { # loop reading lines from standard input push(lines, line) # use stack-like syntax to push the line on the list } while line := pop(lines) do { # loop while lines can be popped off the list write(line) # write the line out } </syntaxhighlight> Using the fail propagation as seen in earlier examples, we can combine the tests and the loops: <syntaxhighlight lang="icon"> lines := [] # create an empty list while push(lines, read()) # push until empty while write(pop(lines)) # write until empty </syntaxhighlight> Because the list collection is a generator, this can be further simplified with the bang syntax: <syntaxhighlight lang="icon"> lines := [] every push(lines, !&input) every write(!lines) </syntaxhighlight> In this case, the bang in {{code|write}} causes Icon to return a line of text one by one from the array and finally fail at the end. {{code|&input}} is a generator-based analog of {{code|read}} that reads a line from [[standard input]], so {{code|!&input}} continues reading lines until the file ends. As Icon is typeless, lists can contain any different types of values: <syntaxhighlight lang="icon">aCat := ["muffins", "tabby", 2002, 8]</syntaxhighlight> The items can included other structures. To build larger lists, Icon includes the {{code|list}} generator; {{code|code=i := list(10, "word")}} generates a list containing 10 copies of "word". Like arrays in other languages, Icon allows items to be looked up by position, e.g., {{code|code=weight := aCat[4]}}. [[Array slicing]] is included, allowing new lists to be created out of the elements of other lists, for instance, {{code|aCat :{{=}} Cats[2:4]}} produces a new list called aCat that contains "tabby" and 2002. Tables are essentially lists with arbitrary index keys rather than integers: <syntaxhighlight lang="icon"> symbols := table(0) symbols["there"] := 1 symbols["here"] := 2 </syntaxhighlight> This code creates a table that will use zero as the default value of any unknown key. It then adds two items into the table, with the keys "there" and "here", and values 1 and 2. Sets are also similar to lists but contain only a single member of any given value. Icon includes the {{code|++}} to produce the union of two sets, {{code|**}} the intersection, and {{code|--}} the difference. Icon includes a number of pre-defined "Cset"s, a set containing various characters. There are four standard Csets in Icon, {{code|&ucase}}, {{code|&lcase}}, {{code|&letters}}, and {{code|&digits}}. New Csets can be made by enclosing a string in single quotes, for instance, {{code|vowel :{{=}} 'aeiou'}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)