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

Appendix A

ABC Quick Reference

COMMANDS

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

SET RANDOM expr

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

PASS

Do nothing

KEYWORD expr KEYWORD ...

Execute user-defined command

KEYWORD

Execute refined command

CHECK test

Check test and stop if it fails

IF test:

     commands

If test succeeds, execute commands;

    no ELSE allowed

SELECT:

Select one alternative:

test: commands

try each test in order

...

(one must succeed;

test: commands

the last test may be ELSE )

WHILE test:

      commands

As long as test succeeds,

     execute commands

FOR name ...IN train:
       commands

Take each item of train in turn

HOW-TO's

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

QUIT

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

RETURN expr

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

REPORT test

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

SUCCEED

The same, report success

FAIL

The same, report failure

EXPRESSIONS AND ADDRESSES

666 , 3.14 , 3.14e-9

Exact constants

expr, expr, . . .

Compound

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)

name

Result of refinement (no permanent effects)

TESTS

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)

name

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

PREDEFINED FUNCTIONS AND PREDICATES

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

Denominator

random

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 not.in 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

THE CHARACTERS

 !"#$%&'()*+,-./
0123456789:;<=>?
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_
`abcdefghijklmno
pqrstuvwxyz{|}~

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