The ABC Programmer's Handbook by Leo Geurts, Lambert Meertens, and Steven Pemberton

Appendix A

ABC Quick Reference


WRITE expr

Write to screen; / before or after expr gives new line

READ address EG expr

Read expression from terminal to address; expr is example of type to be read.

READ address RAW

Read line of text

PUT expr IN address

Put value of expr in address


Start random sequence for random and choice

REMOVE expr FROM list

Remove one item from list

INSERT expr IN list

Insert in right place

DELETE address

Delete location or table entry


Do nothing


Execute user-defined command


Execute refined command

CHECK test

Check test and stop if it fails

IF test:


If test succeeds, execute commands;

    no ELSE allowed


Select one alternative:

test: commands

try each test in order


(one must succeed;

test: commands

the last test may be ELSE )

WHILE test:


As long as test succeeds,

     execute commands

FOR name ...IN train:

Take each item of train in turn


HOW TO KEYWORD . . . : commands

Defines new command KEYWORD . . .

HOW TO RETURN f: commands

Defines new function f with no arguments (returns a value)

HOW TO RETURN f x: commands

Defines new function f with one argument

HOW TO RETURN x f y: commands

Defines new function f with two arguments

HOW TO REPORT pr: commands

Defines new predicate pr with no arguments (succeeds/fails)

HOW TO REPORT pr x: commands

Defines new predicate pr with one argument

HOW TO REPORT x pr y: commands

Defines new predicate pr with two arguments

SHARE name, . . .

(Before commands of how-to) share names

Refinements (after the commands of a how-to)

KEYWORD : commands

Defines command-refinement

name: commands

Defines expression- or test-refinement

Terminating command


Leave command-how-to or command-refinement, or leave ABC


Leave function-how-to or expression-refinement, return value of expr


Leave predicate-how-to or test-refinement, report outcome of test


The same, report success


The same, report failure


666 , 3.14 , 3.14e-9

Exact constants

expr, expr, . . .


name, name, . . .

Naming (may also be used as address)

text @ p

"ABCD"@2 = "BCD" (also address)

text | q

"ABCD"|3 = "ABC" (also address)

text @ p| q

"ABCD"@2|1 = "BCD"|1 = "B"

table[ expr]

Table selection (also address)

"Jan" ,'Feb' ,'Won''t!'

Textual displays (empty: "" or '' )

"value = ` expr`;"

Conversion of expr to text

{1; 2; 2; . . .}

List display (empty: {} )

{1..9; . . .} ,{"a".."z"; . . .}

List of consecutive values

{["Jan"]: 1; ["Feb"]: 2; . . .}

Table display (empty: {} )

f, f x, x f y

Result of function f (no permanent effects)


Result of refinement (no permanent effects)


x > y, x <= y, x >= y, x > y

Order tests

x = y, x <> y

(<> means `not equals')

0 <= d < 10

pr, pr x, x pr y

Outcome of predicate pr (no permanent effects)


Outcome of refinement (no permanent effects)

test AND test AND . . .

Fails as soon as one of the tests fails

test OR test OR . . .

Succeeds as soon as one of the tests succeeds

NOT test

SOME name, . . . IN train HAS test

Sets name, . . . on success

EACH name, . . . IN train HAS test

Sets name, . . . on failure

NO name, . . . IN train HAS test

Sets name, . . . on failure


Functions and predicates on numbers

~ x

Approximate value of x

exactly x

Exact value of x

exact x

Test if x is exact

+ x, x+ y, x- y,- x, x* y, x/ y

Plain arithmetic

x** y

x raised to the power y

root x, n root x

Square root, n-th root

abs x,sign x

Absolute value, sign (= -1, 0, or +1)

round x,floor x,ceiling x

Rounded to whole number

n round x

x rounded to n digits after decimal point

a mod n

Remainder of a on division by n

*/ x

Numerator of exact number x

/* x



Random approximate number r, 0 £ r < 1

e , exp x

Base of natural logarithms, exponential function

log x, b log x

Natural logarithm, logarithm to the base b

pi , sin x, cos x, tan x, arctan x

Trigonometric functions, with x in radians

angle (x, y),radius (x, y)

Angle of and radius to point (x, y)

c sin x, c cos x, c tan x

Similar, with the circle divided into c parts

c arctan x, c angle (x, y)

(e.g. 360 for degrees)

Functions on texts

t^ u

t and u joined into one text

t^^ n

t repeated n times

lower t

lower "aBc" = "abc"

upper t

upper "aBc" = "ABC"

stripped t

t with leading and trailing spaces removed

split t

t split into words

Function on tables

keys table

List of all keys in table

Functions and predicates on trains

# train

Number of items in train

e# train

Number of items equal to e

e in train, e train

Test for presence or absence

min train

Smallest item of train

e min train

Smallest item larger than e

max train, e max train

Largest item

train item n

n-th item

choice train

Random item

Functions on all types

x<> n

x converted to text, aligned left in width n

x>> n

Similar but centred

x>> n

Similar but aligned right



This is the order of all characters that may occur in a text. (The first is a space.)

The ABC Programmer's Handbook by Leo Geurts, Lambert Meertens, and Steven Pemberton