Adding and Subtracting Negative Integers
The Adding and Subtracting
script in the Examples section constructs a transducer, PlusMinusOne,
for adding to and subtracting one from a positive integer. For example,
with PlusMinusOne on the stack, the command apply up 9
returns 10;
and apply
down 10 returns 9. The apply up
commands works for positive integers of any size but the apply down
command does not yield any output for numbers smaller that 1.
The Task: Using the Adding and
Subtracting script as a model, construct a transducer that works for
zero and the infinite set of negative integers. The result, call it PlusMinusOneNeg,
should yield the results below:
apply down 0
-1
apply up -1
0
apply up -1000
-999
apply down
-1000
-1001
etc.
When all is done, see if you can improve the original Adding and
Subtracting script to produce a single transducer that handles both
positive and negative integers with zero.
Hint: This is not as
straight-forward as it may seem. Just combining the positive PlusMinusOne
network with the negative one by composition will not have the intended
effect because the zero in the middle gets lost and 1 maps
directly to -1,
and vice versa. One solution is to operate initially with two stand-in
zeros, say +0 and -0. When
everything else is done, the two temporary zeros can be replaced with
the real zero using the substitute symbol
command:
substitute symbol
"0" for "+0"
substitute
symbol "0" for "-0"