Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Icon (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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'}}.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Icon (programming language)
(section)
Add topic