a
and m
work on strings too
Though it isn't explicitly stated in the spec, a
(add) and m
(multiply), at least in the official Ruby interpreter, also work on strings.
Say you had two cells with strings in them at the end of the tape, and wanted to concatenate and print them. You could do it with c
:
c{{P
However, as long as you don't want to preserve the original value of your first cell, you could append the second string directly onto the first with a
and avoid having to go to the end of the tape:
aP
Now let's say you want to print 20 spaces. The naive way to do it would be to simply hardcode them:
` `P
As long as the cell to the right is free, though, you can save 11 instructions by using m
:
` `{`20`n}mP
Note, however, that due to the way Ruby works, you cannot do this with the order of the cells reversed (i.e. multiply a number by a string); that causes an error.