Hello World in Icon
Your first Icon program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Icon program and get a glimpse of this unique language with its goal-directed evaluation.
The Code
Create a file named hello.icn:
procedure main()
write("Hello, World!")
end
Understanding the Code
This simple program demonstrates Icon’s basic structure:
procedure main()- The entry point of an Icon program, similar tomain()in C or Javawrite("Hello, World!")- Thewriteprocedure outputs text followed by a newlineend- Marks the end of the procedure
Why procedure and not function?
In Icon, procedure is the keyword for defining both procedures (which may not return a value) and functions (which do). The distinction is in how they’re used, not in their declaration.
Running with Docker
The easiest way to run Icon without installing it locally:
| |
What These Commands Do
icont hello.icn- The Icon translator compiles the source file./hello- Runs the resulting executable
The translator produces a stand-alone executable (on Unix systems) that includes everything needed to run.
Running Locally
If you have Icon installed (available in Debian/Ubuntu via apt install icont iconx):
| |
Or compile and run in one step:
| |
The -s flag suppresses the translator messages, and -x executes immediately after translating.
Expected Output
Hello, World!
Alternative Approaches
Using writes() Without Newline
If you want to output without a trailing newline:
procedure main()
writes("Hello, ")
write("World!")
end
Using String Concatenation
Icon uses || for string concatenation:
procedure main()
greeting := "Hello"
target := "World"
write(greeting || ", " || target || "!")
end
Using Multiple Arguments
The write procedure accepts multiple arguments:
procedure main()
write("Hello", ", ", "World", "!")
end
Your First Taste of Icon’s Unique Features
Even this simple program hints at Icon’s philosophy. Let’s explore a slightly more interesting version:
Using Generators
procedure main()
every write("Hello, " || !["World", "Icon", "Everyone"] || "!")
end
This outputs:
Hello, World!
Hello, Icon!
Hello, Everyone!
The ! operator generates each element of the list, and every iterates through all values.
Success and Failure
procedure main()
# This succeeds and prints
if write("Hello, World!") then
write("That worked!")
# write() always succeeds, but find() might fail
if find("World", "Hello, World!") then
write("Found 'World'!")
end
In Icon, expressions can succeed (producing a value) or fail. This is different from returning true/false - failure means the expression produces no value at all.
Key Concepts
- Procedures - Defined with
procedure…endblocks - Entry Point - The
main()procedure is called when the program runs - Output -
write()outputs with newline,writes()without - Compilation -
iconttranslates Icon source to an executable - Execution - The resulting file is a standalone program
The Icon Translator
When you run icont:
$ icont hello.icn
Translating:
hello.icn:
main
No errors
The translator shows:
- Files being processed
- Procedures found in each file
- Any errors (or “No errors” if successful)
The output is an executable file named after your source file (without the .icn extension).
Icon’s Design Philosophy
Even in Hello World, Icon shows its heritage:
- Clean Syntax - Unlike its predecessor SNOBOL, Icon uses familiar structured programming syntax
- Procedures are First-Class - Procedures can be passed as arguments and stored in variables
- Everything is an Expression - Even
write()returns a value (the string it wrote)
Historical Note
Icon was designed by Ralph Griswold at the University of Arizona starting in 1977. It evolved from SNOBOL but adopted a more conventional syntax. The name “Icon” was chosen partly because the word “iconoclastic” described the language’s unconventional approach to evaluation - where expressions can succeed or fail, and generators can produce multiple values.
Next Steps
Continue exploring Icon’s unique features:
- Variables and data types
- Generators and the
everyexpression - String scanning for text processing
- Lists, sets, and tables
- Goal-directed evaluation in depth
- Co-expressions for advanced control flow
Icon will challenge your assumptions about how programming languages work. Its goal-directed evaluation and generator concepts have influenced modern languages including Python, making it both historically important and practically educational.
Welcome to Icon - where success and failure drive your program’s flow!
Running Today
All examples can be run using Docker:
docker pull codearchaeology/icon:latest