# Category:BlooP

BlooP
This programming language may be used to instruct a computer to perform a task.
Listed below are all of the tasks on Rosetta Code which have been solved using BlooP.

BlooP and it's sister language Floop are 'theoretical' languages with several implementations, created by Douglas Hofstadter in his book Gödel, Escher, Bach, to demonstrate the halting problem.

The name BlooP is short for Bounded Loop, the primary structure of the language. FlooP is identical to BlooP in every way except that it also sorts unbounded loops. FlooP is Turing complete, where BlooP is not. There is also a third, impossible language called GlooP, which can solve the halting problem.

BlooP and FlooP have only 1 data type: non-negative integers (natural numbers). There are also only 2 variables (except parameters to procedures): `OUTPUT`, which is the value returned by a procedure, and `CELL(i)`, which is an unbounded series of numbers. Some implementations also add string literals, which can only be used by an added `PRINT` instruction.

The operators that exist in BlooP and FlooP are:

• `<=` or `⇐` : Assignment
• `+` : Addition
• `*` or `×`: Multiplication
• `>` : Greater Than
• `<` : Less Than
• `=` : Equals

Note that subtraction and division are conspicuously missing, as are modulus and exponentiation. These can and must be defined in terms of loops and the provided operators.

## Example Code

The best way to explain BlooP is really just to look at some examples, so here are a few:

Find the factorial of N: <lang BlooP> DEFINE PROCEDURE FACTORIAL [N]: BLOCK 0: BEGIN

```       OUTPUT <= 1;
CELL(0) <= 1;
LOOP AT MOST N TIMES:
BLOCK 1: BEGIN
OUTPUT <= OUTPUT * CELL(0);
CELL(0) <= CELL(0) + 1;
BLOCK 1: END;
```

BLOCK 0: END. </lang>

subtraction (this is the only way to do it, since it must be defined in terms of addition): <lang BlooP> DEFINE PROCEDURE MINUS [M,N]: BLOCK 0: BEGIN

```       OUTPUT <= 0;
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. </lang>

Nim game in BlooP. Since user input is not possible in most implementations, this version uses a procedure which takes 3 numbers. <lang BlooP> DEFINE PROCEDURE DIVIDE [A,B]: BLOCK 0: BEGIN

``` IF A < B, THEN:
QUIT BLOCK 0;
CELL(0) <= 1;
OUTPUT <= 1;
LOOP AT MOST A TIMES:
BLOCK 2: BEGIN
IF OUTPUT * B = A, THEN:
QUIT BLOCK 0;
OUTPUT <= OUTPUT + 1;
IF OUTPUT * B > A, THEN:
BLOCK 3: BEGIN
OUTPUT <= CELL(0);
QUIT BLOCK 0;
BLOCK 3: END;
CELL(0) <= OUTPUT;
BLOCK 2: END;
```

BLOCK 0: END.

DEFINE PROCEDURE MINUS [A,B]: BLOCK 0: BEGIN

``` IF A < B, THEN:
QUIT BLOCK 0;
LOOP AT MOST A TIMES:
BLOCK 1: BEGIN
IF OUTPUT + B = A, THEN:
QUIT BLOCK 0;
OUTPUT <= OUTPUT + 1;
BLOCK 1: END;
```

BLOCK 0: END.

DEFINE PROCEDURE MODULUS [A,B]: BLOCK 0: BEGIN

``` CELL(0) <= DIVIDE[A,B];
OUTPUT <= MINUS[A,CELL(0) * B];
```

BLOCK 0: END.

DEFINE PROCEDURE PLAYER_TURN [TOKENS_LEFT, TAKE]: BLOCK 0: BEGIN

``` CELL(0) <= TAKE;
```
``` IF TAKE > 3, THEN:
BLOCK 1: BEGIN
CELL(0) <= MODULUS [TAKE, 3] + 1;
PRINT ['take must be between 1 and 3. setting take to ', CELL(0), '.'];
BLOCK 1: END;
```
``` IF TAKE < 1, THEN:
BLOCK 2: BEGIN
CELL(0) <= 1;
PRINT ['take must be between 1 and 3. setting take to 1.'];
BLOCK 2: END;
```
``` OUTPUT <= MINUS [TOKENS_LEFT, CELL(0)];
```
``` PRINT ['player takes ', CELL(0), ' tokens.'];
PRINT ['tokens remaining: ', OUTPUT];
PRINT [];
```

BLOCK 0: END.

DEFINE PROCEDURE COMPUTER_TURN [TOKENS_LEFT]: BLOCK 0: BEGIN

``` CELL(0) <= MODULUS [TOKENS_LEFT, 4];
OUTPUT <= MINUS [TOKENS_LEFT, CELL(0)];
```
``` PRINT ['computer takes ', CELL(0), ' tokens.'];
PRINT ['tokens remaining: ', OUTPUT];
PRINT [];
```

BLOCK 0: END.

DEFINE PROCEDURE PLAY_GAME [FST, SEC, THD]: BLOCK 0: BEGIN

``` CELL(0) <= FST;
CELL(1) <= SEC;
CELL(2) <= THD;
OUTPUT <= 12;
```
``` LOOP 3 TIMES:
BLOCK 1: BEGIN
OUTPUT <= PLAYER_TURN [OUTPUT, CELL(0)];
CELL(0) <= CELL(1);
CELL(1) <= CELL(2);
```
```   OUTPUT <= COMPUTER_TURN [OUTPUT];
BLOCK 1: END;
```
``` PRINT ['computer wins!'];
```

BLOCK 0: END.

PLAY_GAME [2,1,1]; </lang>

Output:
```> PLAYER TAKES 2 TOKENS.
> TOKENS REMAINING: 10
>
> COMPUTER TAKES 2 TOKENS.
> TOKENS REMAINING: 8
>
> PLAYER TAKES 1 TOKENS.
> TOKENS REMAINING: 7
>
> COMPUTER TAKES 3 TOKENS.
> TOKENS REMAINING: 4
>
> PLAYER TAKES 1 TOKENS.
> TOKENS REMAINING: 3
>
> COMPUTER TAKES 3 TOKENS.
> TOKENS REMAINING: 0
>
> COMPUTER WINS!
=> 0
```

## Pages in category "BlooP"

The following 4 pages are in this category, out of 4 total.