ABC is an imperative language originally designed as a
replacement for BASIC: interactive, very easy to learn, but
structured, high-level, and easy to use. ABC has been designed
iteratively, and the present version is the 4th iteration. The
previous versions were called B (not to be confused with the
predecessor of C).
It is suitable for general everyday programming, the sort of
programming that you would use BASIC, Pascal, or AWK for. It is not
a systems-programming language. It is an excellent teaching
language, and because it is interactive, excellent for prototyping.
It is much faster than Unix ‘bc’ for doing quick calculations.
ABC programs are typically very compact, around a quarter to a
fifth the size of the equivalent Pascal or C program. However, this
is not at the cost of readability, on the contrary in fact (see the
examples below).
ABC is simple to learn due to the small number of types in the
language (five). If you already know Pascal or something similar
you can learn the whole language in an hour or so. It is easy to
use because the data-types are very high-level.
The five types are:
- numbers: unbounded length, with exact arithmetic the rule
- texts (strings): also unbounded length
- compounds: records without field names
- lists: sorted collections of any one type of items (bags or
multi-sets) - tables: generalised arrays with any one type of keys, any one
type of items (finite mappings).
The Advantages of ABC as an Introductory Programming Language
ABC leaves time to teach the principles
With a programming language like Pascal, the experience is that most
of the time in class is spent on the details of the language, leaving
too little time to teach about what really matters: the principles of
programming. Quite possibly, a one-term course may not even get round
to introducing pointers. With ABC, the full language can be covered
in a few hours, leaving ample time to treat interesting and
instructive examples of programming in detail.
ABC is good for teaching the principles
Unlike BASIC, ABC is a language that offers strong support for
structured programming, even better than Pascal. Refinements, for
top-down stepwise program development, are an integral part of the
language. Because of the powerful data-types of ABC, including tables
(associative arrays), algorithms can be written at a problem-oriented
level of abstraction. There is no GOTO statement in ABC, and
expressions do not have side-effects.
ABC lets you choose interesting examples
With many other languages, including BASIC and Pascal, it is always
hard to pick interesting examples. Especially early in the course,
when only numeric types and arrays have been introduced, it is very
problematic to handle more challenging problems than say computing the
average of a sequence. The lack of procedures at the early stages may
also cause the students to develop bad programming habits. With ABC,
you can treat interesting examples right from the start. Programming
problems involving texts (strings) are as easy to solve in ABC as
problems with numbers. Programs in ABC are often five to twenty times
as short as their counterparts coded in BASIC or Pascal. There is no
distinction whatsoever in ABC between a program and a procedure.
ABC allows you to set challenging assignments
For basically the same reasons, you can set assignments you would not
even think about if the students had to program in BASIC or Pascal.
Here are some examples of instructive problems that students can solve
after only a few weeks, using ABC:
- Pretty-printing a genealogy tree, given a table with the
parent-child relation. - Finding the shortest path through a maze.
- Text-formatting, including features like adjustment,
indenting, paragraphs, and producing a table of contents and
an index.
ABC is student-friendly
ABC is fully interactive, doing away with the edit-compile-run cycle.
The language has strong type checking without declarations. The
error messages are to the point and self-explanatory, both for syntax
and for run-time errors, always showing the spot in the source program
where the error occurred. The dedicated editor of ABC, which knows the
ABC syntax and which has a multiple UNDO, greatly reduces both the
opportunities for making syntax errors (no more unbalanced
parentheses) and the time needed for entering a program, thus
demanding less time from the instructor for helping with trivial
problems. ABC programs are automatically displayed pretty-printed,
with indentation showing the logical grouping. The ABC environment is
persistent, meaning that variables (including tables) survive a
session and remain accessible until explicitly deleted.
ABC: Some Simple Examples
The (second) best way to appreciate the power of ABC is to see some
examples (the first is to use it). In what follows, >>> is the
prompt from ABC.
There are five types in the language: two basic — Numbers and Texts — and three structured — Compounds, Lists, and Tables.
Numbers
Numbers are unbounded, and stored exact:
>>> WRITE 2**1000 107150860718626732094842504906000181056140481170553360744375038837 035105112493612249319837881569585812759467291755314682518714528569 231404359845775746985748039345677748242309854210746050623711418779 541821530464749835819412673987675591655439460770629145711964776865 42167660429831652624386837205668069376 >>> PUT 1/(2**1000) IN x >>> WRITE 1 + 1/x 107150860718626732094842504906000181056140481170553360744375038837 035105112493612249319837881569585812759467291755314682518714528569 231404359845775746985748039345677748242309854210746050623711418779 541821530464749835819412673987675591655439460770629145711964776865 42167660429831652624386837205668069377
Non-exact numbers use machine-precision:
>>> WRITE root 2<br /> 1.414213562373095<br />
Texts
Texts (strings of characters) are also unbounded:
>>> PUT ("ha " ^^ 3) ^ ("ho " ^^ 3) IN laugh<br /> >>> WRITE laugh<br /> ha ha ha ho ho ho <br /><br /> >>> WRITE #laugh<br /> 18<br /><br /> >>> PUT "Hello! "^^1000 IN greeting<br /> >>> WRITE #greeting<br /> 7000<br /><br /> >>> WRITE greeting|4<br /> Hell<br /><br /> >>> WRITE greeting@4|3<br /> lo!<br /><br />
Lists
Lists are sorted lists of values of any one other type:
>>> WRITE {1..10}<br /> {1; 2; 3; 4; 5; 6; 7; 8; 9; 10}<br /> >>> PUT {1..10} IN l<br /> >>> REMOVE 5 FROM l<br /> >>> INSERT 4 IN l<br /> >>> INSERT pi IN l<br /> >>> WRITE l<br /> {1; 2; 3; 3.141592653589793; 4; 4; 6; 7; 8; 9; 10}<br />
You can have lists of any type, so here is a list of lists:
>>> PUT {} IN ll<br /> >>> FOR i IN {1..3}:<br /> INSERT {1..i} IN ll<br /> >>> WRITE ll<br /> {{1}; {1; 2}; {1; 2; 3}}<br /> >>> FOR l IN ll:<br /> WRITE l /<br /> {1}<br /> {1; 2}<br /> {1; 2; 3}<br /> >>> WRITE #ll<br /> 3<br /><br />
Compounds
Compounds are like records or structures, but without field names:
>>> PUT ("Square root of 2", root 2) IN c<br /> >>> WRITE c<br /> ("Square root of 2", 1.414213562373095)<br /> >>> PUT c IN name, value<br /> >>> WRITE name<br /> Square root of 2<br /> >>> WRITE value<br /> 1.414213562373095<br /><br />
Tables
Tables resemble arrays:
>>> PUT {} IN tel<br /> >>> PUT 4054 IN tel["Jennifer"]<br /> >>> PUT 4098 IN tel["Timo"]<br /> >>> PUT 4134 IN tel["Guido"]<br /><br /> >>> WRITE tel["Jennifer"]<br /> 4054<br /><br />
You can write all ABC values out. Tables are kept sorted on the keys:
>>> WRITE tel<br /> {["Guido"]: 4134; ["Jennifer"]: 4054; ["Timo"]: 4098}<br />
The keys function returns a list:
>>> WRITE keys tel<br /> {"Guido"; "Jennifer"; "Timo"}<br /><br /> >>> FOR name IN keys tel:<br /> WRITE name, ":", tel[name] /<br /> Guido: 4134<br /> Jennifer: 4054<br /> Timo: 4098<br />
ABC: Program Examples
The (second) best way to appreciate the power of ABC is to see some
examples (the first is to use it). In the examples, >>> is the
prompt from ABC.
Some of these articles are followed by a link to a
source file for the program.
These source files were produced by using the ‘abc -p’ option for packing a
workspace. To unpack it in your own workspaces, save the file as F, choose the
name of your new workspace W, and say:
abc -u -w W F
(you may freely choose the names W and F, but avoid using an
existing workspace name for W).
- A Telephone List
- A Cross-reference Indexer
- An Adventure Game (source)
- Grammar Analysis (source)
- Logic Programming
- Approximate Numbers
- The source of a version of Gerry Weizenbaum’s Eliza program, as presented in
the ABC book. For more details of how the program works, see the book.To use Eliza interactively, just run the command ELIZA.
To watch Eliza talking to someone else, run the command
SESSION grammar
where ‘grammar’ is some grammar to generate English sentences.
There is only one such grammar supplied, paranoid, so you say:SESSION paranoid
If the grammar is empty, then Eliza tries to psychoanalyse herself:
SESSION {}
To quote from her interacting with herself:
> Do you enjoy this sort of interaction?
What a question!
<br /><br />
