|
Icon occurs as super high-level programming language featuring goal directed execution and fantabulous facilities for managing strings and textual patterns. These are related to SNOBOL, a string processing language. Icon is non object-oriented, however an object-oriented extension known as Idol was developed inside 1996 which eventually became Unicon.
Basic syntax
A icon language is dervived from either a Algol-class of structured programming languages, and so has syntax similar to C or Pascal. Icon is virtually all similar to Pascal, applying := syntax for assignments, a procedure keyword & similar syntax. Then again, Icon utilizes C-style brackets for structuring execution groups, & software download run by running off the procedure known as "main".
Within numerous ways Icon too shares features using virtually all scripting programming languages; variables do not keep close at h& to become declared, types come cast automatically, & totals may be converted to strings and back automatically. An additional feature commons to several scripting languages, but non everthing, is the want of the line-ending character; inside Icon lines not ended by the semicolon
make their way all over by an silent semicolon in case it add up.
Procedures come a basiOne hundred building blockades of Icon computer software, & although it utilise Pascal naming it work further prefer C functions & potty link to values; no function keyword around Icon.
procedure doSomething(aString)
write(aString)
end
Goal-directed execution
One of Icon's key construct is to vary control structures from either ones according to boolean algebra, to ones according to "success" or even "failure". Under this model elementary comparisons such as whenever the < b don't mean "if the operations to the right evaluate to true" when a would under virtually all languages, instead it means something additional such as "if the operations to the right succeed". Therein example a < operator succeeds if the comparison is true, so the end result is identical between Icon and other languages. Things become slighly more interesting when you consider in case the < b < c, the virtually all common nature and severity of comparison that can't become directly stated inside most languages.
A utility of this construct becomes lot clearer when you assume real-globe examples. Since Icon utilizes profits or even failure for 100% flow control, this elementary code:
whenever the := page through() so write(the)
May copy 1 line of the standard input to standard output. What's interesting all about this lesson is that a code may function potentially in case a scroll through() stimulates an error, e.g., whenever a file doesn't survive. Therein experience the statement a := scroll through() might fail, & write might just non become known as.
Profits & failure come passed "up" across functions, meaning that a failure in the nested work might are causal agents for the functions calling it to fail too. For example, you may write the program to copy an entire input file to output inside one line:
patch write(page through())
Once a page through() comm& fails, at a prevent of file e.g., a failure is passed higher a chain and write() may fail too. a spell, existence a control structure, ends in failure, meaning it ends after the file is empty. For comparison, assume the similar case written around Java-based pseudocode:
try
} catch (Exception e)
In that outbreak there are deuce comparisons required, of these for prevent of file (EOF) & a second for completely more errors. Since Java doesn't allow errors to exist when equated when logic elements, as under Icon, a prolonged try/catch syntax must exist as utilized instead. Endeavor jams too inflict the performance penalty for just utilizing the babies, potentially whenever there is no error occurs, the distributed cost that Icon avoids.
Icon refers to this construct when goal-purposive execution, on to the way that execution continues until a select few goal is reached. In a lesson above a goal is to scroll through a entire file; the underst& command continues to succeed while there exists extra reference to exist as review, and fails when there international relations and security network't. A goal is so coded directly in the language, instead of applying statements checking link to codes or even similar constructs.
Generators
Expressions inside Icon typically link to one value, for example, x < 5 may evaluate & go to profits or even failure. All the same numbers of of the examples beneath depend on the fact that many expressions don't immediately go to profits or even failure, giving values meanwhile. This causes a examples by having each & to; each induces to to prove my point to link to values until it fails.
This occurs as key construct around Icon, called generators. Generators cause good deal of the loop functionality in the language, however clean sol other directly; the software engineer doesn't write a loop & so pull out and compare values, Icon might run 100% of this for wise shoppers.
Icon includes many generator-builders. A alternator syntax allows the series of things to exist as generated around sequence until of these fails: Single | "hello" | x < 5 may generate "1", "hello", & "5" whenever x is to a lesser degree than Five. Alternators may be review when "or" inside several events, e.g.:
in case y < (x | 5) then write("y=", y)
may write out a value of y in case these are little than x or even Phoebe. Internally Icon checks each value from either left to best until of these succeeds or even the names empties & it is restored a failure. Remember that functions might non become known as unless a calls in don't fail, then this lesson may be shortened to:
write("y=", (x | Five) > y)
A second elementary generator is the to, which generates lists of whole number; each write(1 to 10) may launder exactly what it seems to. A bang syntax generates each item of the names; each write(!aString) might output both character of aString in the up to date line.
To demonstrate a power of this construct, assume string operations. Virtually all languages include the work called locate or even indexOf that is restored the location of a string inside an additional. Assume:
s = "All the world's a stage. And all the men and women merely players";
i personally = indexOf("the", s)
This code might link to Quartet, a position of a number 1 occurrence of the word "the". For a next time of "the" an replacement form must become utilized, i = indexOf("the", s, Fivesome), a Quintet at a prevent saying it should look from either positiin Five on. Sequentially to extract all the occurrences of "the", the loop must become utilized...
s = "All the world's a stage. And all the men and women merely players";
i personally = indexOf("the", s)
patch we != -Single
Under Icon a call for work occurs as generator, & might link to a next case of a string both period these are resumed prior to eventually failing when it lives the prevent of the string. A equivalent code under Icon may be written:
s := "All the world's a stage. And all the men and women merely players"
each write(buy("the",s))
Locate might link to a stock of the next time of "the" both instance these are resumed by each, sooner or later passing a prevent of the string & failing. When in a anterior case, this may induce write to fail, & the (1-line) each loop to exit.
Naturally there are days withwithin which that your family deliberately obviously buy the string fallowing the bit of point in input, e.g., you may exist as scanning a document containing information in multiple columns. Goal-purposive execution works on text too, & may be utilized this way:
write(5 < find("the", s))
A position may lone become returned whenever "the" appears when position Cinque, a comparison might fail otherwise, passing that failure to write() when prior to. There exists of these little "trick" to this code that needs to exist as considered: comparisons link to a correct hand effect, and so these are crucial to put the buy on the right hand side of the comparison. In case it was situated on the left, V would become written.
Icon adds many control structures for looping across generators. A each operator is similar to spell, looping across each item returned by the generator & exiting in failure:
each k := we to j do
doSomething(k)
Do you need each instead of the patch loop therein outbreak?
Because patch re-evaluates a number one symptom,
however each green groceries 100% resolutions.
A each syntax actually injects values into the work within a fashion similar to impedes under Smalltalk. For example, a above loop may be re-written this way:
each write(someFunction(we to j))
Users potty build fresh generators well using the suspend keyword:
procedure findOnlyOdd(pattern, theString)
each we := locate(pattern, theString) do
whenever we % Two = Unity so suspend i
end
This lesson loops assibilate theString applying buy to search pattern. After of these is noticed, & a position is possibly, a location is returned from either a work using suspend. Unlike link to, suspend writes down in which these are in the internal generators too, letting it to pick higher in which it left hit on the next iteration.
Strings
Within keeping by using its script-prefer functionality, Icon adds the total of features to produce working by using strings more comfortable. Virtually all notable among these is the scanning patterns, which repeatedly calls functions in the string:
s ? write(locate("the"))
occurs as short form of the examples shown earliest. Therein instance a subject of the call for work is positioned outside a parameters before of the wonder-mark. Icon functions come deliberately (when opposed to automatically) written to identify a subject inside parameter lists & allow the babies to become pulled call at this fashion.
Other structures
Icon strings come only lists of characters, similar to their partners inside C. Icon as well allows a user to well construct their have lists (or even arrays):
aCat := ["muffins", "tabby", 2002, 8]
the things in a listing may be of any sort, including more structures. To quickly build big lists, Icon includes a list generator; we := listing(Ten, "word") generates the names containing X copies of "word".
Rather arrays around more languages, Icon allows things to become scanned higher by position; weight := aCat[4]. Likewise remember a bang-syntax, each write(!aCat) might print retired iv lines, for each a single sustaining one element. Icon includes fold-prefer functions, click & pop to allow a children to form the basis of stacks & queues.
Icon too includes functionality for sets & tables (called hashes, associatory arrays, lexicon, etc.), however a syntax for creating the babies is non when nice as a listing:
symbols := table(Zero)
symbols["there"] := 1
symbols["here"] := 2
This code creates a table that may utilise zero when the default value of any unknown key. It so adds ii things into it, by using a keys "there" & "here", & values One & Two. Note that virtually all modern scripting languages combine lists & tables into of these feature, letting a user to refer things by position or even title, whenever it has one. This vary would seem to fresh higher a Icon syntax too.
|