Context
We already wrote some code to move a value - copy that destroys source.To copy a value, we need to "destroy-copy twice" the target value, and optionally move one of the copies to the initial location.
So, to copy A we need to add 1 to the next 2 cells, A times - first cell will then be null; and move contents of third cell to first one.
Initial state
- Memory: A, 0, 0
- Cursor: first cell
- Input: any
Process
- While first cell is not null, decrease first and increase second / third by 1, and move back to first
- Loop invariant: second cell = third cell, and first+second = A
- When first cell equals 0, then second and third equals A
- Move third cell value to first one
Code
[->+>+<<]>>[-<<+>>]<<
Final state
- Memory: A, A, 0
- Cursor: first cell
- Input: any
- Output: unchanged
Aucun commentaire:
Enregistrer un commentaire