For the description of the eight elementary BF commands, read the introduction page.

[-](Of course, it is rather arbritrary what you call most simple. A program consisting of just a single BF command is even simpler than the above program. But in some way, I feel you can hardly call such a program a program.) The next "most simple" program consist of "moving" a positive or zero value from a memory location to the next memory location. With "moving" we mean that afterwards, the current location has become zero, and the next is increased with the original value from the current location. It looks like:

[ > + < - ](While the value in the current memory location is still larger than zero, move to the next memory location, increment this one, move back, and decrement the current location.)

To move a value from a given source location to a given destination location, the program consists of:

- move memory pointer to the source location
- "[" command
- move memory pointer to the destination location
- "+" command
- move memory pointer to the source location
- "-" command
- "]" command

We can even make this more readable if we would definemove(s,d)=to(s)[to(d)+to(s)- ]

Likewise, we could define a program which moves the value from one location to two locations, as follows:for(a)=to(a)[next(a)=to(a)-]move(s,d)=for(s)to(d)+next(s)

Now what if we would like to copy the value from a location into another location? The solution is simple, just move it to that location and some temporary location, and then move the value from the temporary location back to its original location. The copy program can be described with:move2(s,d1,d2)=for(s)to(d1)+to(d2)+next(s)-]

You can think yourself of a smart way of copying a value from one location to two other locations. In all the above examples we have silently assumed that the destination location contained the value zero. What if it did not? Exactly, then we would not have been moving and copying, but adding to the value already present.copy(s,d,t)=move2(s,d,t) move(t,s)

This program assumes that the destination locationdouble(s,d)=move2(s,d,d)

If the notation *constant(n)* stands for the program
which addes the constant *n* to the current memory
location, that we can describe multiplying with a constant
as:

As multiplying can be seen as repeated adding, it is also very simply to see, how two numbers can be multiplied.multiplyconst(s,d,n)=for(s)to(d) constant(n)next(s)

To raise a number to a certain power, is not so complicated, as raising to a certain power, is nothing else than repeated multiplication:times(s1,s2,d,t)=for(s1)copy(s2,d,t)next(s1)zero(s2)

Dividing however, is a whole different story, far beyond an introductionary course.power(x,p,d,t1,t2,t3)=to(d)+for(p) copy(x,t1) times(d,t1,t2,t3) move(t2,d) next(p) zero(x)

In principle each if-then-else statement can be written as two if-statements where the condition for the second if-statement is whether the first condition was not true. To implement this, we need an temporary memory location, which is set to one on for hand, and set to zero in the then part of the statement. The idea is used in the following set of macros for the if-then-else statement:zero(a)=to(a)[-]if(a)=to(a)[endif(a)=zero(a)]

Note that the last macro can also be defined as:ifelse(a,t)=inc(t) if(a) dec(t)else(a,t)=endif(a) if(t)endelse(t)=endif(t)

endelse(t)=dec(t)]

To implement the logical-and operation we need an if-statement. It is defined with the following macro:or(s1,s2,d)=move(s1,d) move(s2,d)

Theand(s1,s2,d)=if(s1) move(s2,d) endif(s1) zero(s2)

For the logical-not operation the same trick as with the if-then-else statement can be used. See the following macro:

not(s,d)=inc(d) if(s) dec(d) endif(s)

(Note that the above definition does return the results insubtractMinimum(x1,x2,t1,t2,t3)=copy(x1,t1,t3) copy(x2,t2,t3) and(t1,t2,t3) to(t3)[zero(t3) dec(x1) dec(x2) copy(x1,t1,t3) copy(x2,t2,t3) and(t1,t2,t3) to(t3)]

Although the abovenotEqual(x1,x2,d,t1,t2)=subtractMinimum(x1,x2,d,t1,t2) or(x1,x2,d)Equal(x1,x2,d,t1,t2)=NotEqual(x1,x2,t1,d,t2) not(t1,d)Greater(x1,x2,d,t1,t2)=subtractMinimum(x1,x2,d,t1,t2) zero(x2) move(x1,d)Less(x1,x2,d,t1,t2)=subtractMinimum(x1,x2,d,t1,t2) zero(x1) move(x2,d)GreaterOrEqual(x1,x2,d,t1,t2)=inc(x1) Greater(x1,x2,d,t1,t2)LessOrEqual(x1,x2,d,t1,t2)=inc(x2) Less(x1,x2,d,t1,t2)

subtractMinimum(x1,x2,t1,t2,t3)=for(x1) copy(x2,t1,t2) ifelse(t1,t2) dec(x2) else(t1,t2) inc(t3) endelse(t2) next(x1) move(t3,x1)

isZero(s,d)=not(s,d)isOne(s,d)=if(s) dec(s) isZero(s,d) endif(s)isTwo(s,d)=if(s) dec(s) isOne(s,d) endif(s)isThree(s,d)=if(s) dec(s) isTwo(s,d) endif(s)....

Below we show how a number of arrays with unlimited size can be realized. On forhand we have to decide how many of these arrays we need, and where they start. All memory locations above the one where they start are reserved for the arrays, and cannot used for any other usage. We basically, need two procedures: one for getting a value from an array, and one for setting a value in an array. Both procedures have five parameters:

: Specifies the starting point (base) of the arrays.*b*: Specifies the number of arrays.*n*: Specifies the array to be used. (The first array has number zero.)*a*: Specifies the index of cell of the array to which the operation applies. (The index of the first cell is zero)*i*: For getting it is the memory location in which the value from the array should be stored into, while for setting it is the memory location from which the value is copied into the array.*v*

In which the expressionsetarray(b,n,a,i,v)=copy(i,b+1,b) copy(v,b+2,b) for(b+1) move(b,b+n+3) move(b+1,b+n+4) move(b+2,b+n+5) right(n+3) inc(b) next(b+1) zero(a) move(b+2,a) for(b) left(n+3) move(b+n+3,b) next(b)getarray(b,n,a,i,v)=copy(i,b+1,b) for(b+1) move(b,b+n+3) move(b+1,b+n+4) right(n+3) inc(b) next(b+1) copy(a,b+1,b+2) for(b) left(n+3) move(b+n+3,b) move(b+n+4,b+1) next(b) move(b+1,v,b)

home and email