BLooP and FLooP are simple programming languages designed by Douglas Hofstadter to illustrate a point in his book Gödel, Escher, Bach. BLooP is a non-Turing-complete programming language whose only control flow structure is a bounded loop. This language can only express primitive recursive functions. FLooP is identical except that it supports unbounded loops; it is a Turing-complete language. These can be regarded as primitive models of computation.
Note: The only variables are output (the return
value of the procedure) and cell(i) (an
infinite array of integers). The only operators are ⇐
(assignment), + (addition), ×
(multiplication), < (less-than), >
(greater-than) and = (equals).
DEFINE PROCEDURE ''FACTORIAL'' [N]:
BLOCK 0: BEGIN
OUTPUT ⇐ 1;
CELL(0) ⇐ 1;
LOOP N TIMES:
BLOCK 1: BEGIN
OUTPUT ⇐ OUTPUT × CELL(0);
CELL(0) ⇐ CELL(0) + 1;
BLOCK 1: END;
BLOCK 0: END.
Subtraction function (this is not a built-in operation):
DEFINE PROCEDURE ''MINUS'' [M,N]:
BLOCK 0: BEGIN
IF M < N, THEN:
QUIT BLOCK 0;
LOOP AT MOST M + 1 TIMES:
BLOCK 1: BEGIN
IF OUTPUT + N = M, THEN:
ABORT LOOP 1;
OUTPUT ⇐ OUTPUT + 1;
BLOCK 1: END;
BLOCK 0: END.
BLooP and FLooP are simple programming languages designed by Douglas Hofstadter to illustrate a point in his book Gödel, Escher, Bach. BLooP is a non-Turing-complete programming language whose only control flow structure is a bounded loop. This language can only express primitive recursive functions. FLooP is identical except that it supports unbounded loops; it is a Turing-complete language. These can be regarded as primitive models of computation.
The only variables are output (the return value of the procedure) and cell(i) (an infinite array of integers). The only operators are ⇐ (assignment), + (addition), × (multiplication), < (less-than), > (greater-than) and = (equals).
DEFINE PROCEDURE ''FACTORIAL'' [N]:
BLOCK 0: BEGIN
OUTPUT ⇐ 1;
CELL(0) ⇐ 1;
LOOP N TIMES:
BLOCK 1: BEGIN
OUTPUT ⇐ OUTPUT × CELL(0);
CELL(0) ⇐ CELL(0) + 1;
BLOCK 1: END;
BLOCK 0: END.
Subtraction function (this is not a built-in operation):
DEFINE PROCEDURE ''MINUS'' [M,N]:
BLOCK 0: BEGIN
IF M < N, THEN:
QUIT BLOCK 0;
OUTPUT ⇐ 0;
LOOP AT MOST M + 1 TIMES:
BLOCK 1: BEGIN
IF OUTPUT + N = M, THEN:
ABORT LOOP 1;
OUTPUT ⇐ OUTPUT + 1;
BLOCK 1: END;
BLOCK 0: END.
|
|