BCPL - Examples

Examples

These complete and compilable examples are from Martin Richards′ BCPL distribution.

Print factorials:

GET "LIBHDR" LET START = VALOF $( FOR I = 1 TO 5 DO WRITEF("%N! = %I4*N", I, FACT(I)) RESULTIS 0 )$ AND FACT(N) = N = 0 -> 1, N * FACT(N - 1)

Count solutions to the N queens problem:

GET "LIBHDR" GLOBAL $( COUNT: 200 ALL: 201 )$ LET TRY(LD, ROW, RD) BE TEST ROW = ALL THEN COUNT := COUNT + 1 ELSE $( LET POSS = ALL & ~(LD | ROW | RD) UNTIL POSS = 0 DO $( LET P = POSS & -POSS POSS := POSS - P TRY(LD + P << 1, ROW + P, RD + P >> 1) )$ )$ LET START = VALOF $( ALL := 1 FOR I = 1 TO 12 DO $( COUNT := 0 TRY(0, 0, 0) WRITEF("%I2-QUEENS PROBLEM HAS %I5 SOLUTIONS*N", I, COUNT) ALL := 2 * ALL + 1 )$ RESULTIS 0 )$

Read more about this topic:  BCPL

Famous quotes containing the word examples:

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)