123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- \ drop elements from the stack silently using `drop`
- 1 .s drop .s
- \ duplicate top element using `dup`
- 1 .s dup .s drop drop .s
- \ copy second element from top to the top of the stack
- 1 2 4 over .s
- \ swap first 2 elements from the top of the stack using `swap`
- 1 2 .s swap .s drop drop .s
- \ left shift first 3 elements on the stack. rotate first 3 elements on
- \ the stack so that lowest will be on top.
- 1 2 3 rot . . . .s
- \ swap and drop more
- 1 2 3 4 .s 2swap .s 2drop 2drop
- \ --> <4> 1 2 3 4 <4> 3 4 1 2 ok
- \ remove second element from top of the stack using `nip`
- 1 2 3 nip .s . . .s
- \ equivalent to
- 1 2 3 swap drop .s . . .s
- \ nip more
- 1 2 3 4 2nip .s . . .s
- \ copy top element and then swap it with the second element
- 1 2 3 4 tuck .s
- \ equivalent to
- 1 2 3 4 swap over .s
- \ 1 2 3 --> 3 2 1
- 1 2 3 swap rot .s . . .
- \ 1 2 3 --> 1 2 3 2
- 1 2 3 swap tuck .s . . . .
- \ 1 2 3 --> 1 2 3 3
- 1 2 3 dup .s . . . .
- \ 1 2 3 --> 1 3 3
- 1 2 3 nip dup .s . . .
- \ 1 2 3 --> 2 1 3
- 1 2 3 rot swap .s . . .
- \ 1 2 3 4 --> 4 3 2 1
- \ 2swap affects a range of 4 values, bringing values at position 3 and
- \ 4 to the front.
- 1 2 3 4 swap 2swap swap .s . . . .
- \ 1 2 3 --> 1 2 3 1 2 3
- 1 2 3
- rot \ get the 1 to the top
- dup \ copy the 1
- 2swap \ move the 1 back to the beginning
- \ --> 1 1 2 3
- rot rot \ get the 2 to the top
- dup \ copy the 2
- 2swap \ put the 2 back in place at the 2. position from the bottom
- rot rot \ correct rotation state
- \ --> 1 2 1 2 3
- dup \ copy the 3
- 2swap \ put the 3 in its place
- rot \ correct rotation state
- .s
- . . . . . .
- .s
- \ This approach is using the fact, that we can reach 4 elements wide
- \ using `2swap` and `rot` only affects the first 3 positions. Anything
- \ we move to the 4. position will remain untouched by `rot`. It is
- \ basically "stored".
- \ 1 2 3 4 --> 1 2 3 4 1 2
- \ + We cannot remove anything, because all elements are unique and we
- \ want to remain general.
- \ + We cannot add anything, while `1` is not at the lowest position,
- \ because we only have range 4 effects with our word.
- \ + We need to add 1 and 2 while 1 is at the lowest position. We have
- \ no word for this.
- \ + Unsure how to solve this.
- \ + Here is a cheap shot "solution":
- 1 2 3 4
- 1 2 .s . . . . . .
- \ 1 2 3 -->
- 1 2 3 . . . .s
- \ 1 2 3 --> 1 2 3 4
- 1 2 3 drop .s . . .
- \ 1 2 3 --> 1 3
- 1 2 3 nip .s . .
- \ combine with artihmetic words
- 5 dup * .
- \ Exercise: Write 17^3 and 17^4 in Forth, without writing 17 more than
- \ once.
- 17 dup dup * * .s .
- 17 dup dup dup * * .s .
- \ Write a piece of Forth code that expects two numbers on the stack (a
- \ and b, with b on top) and computes (a-b)(a+1).
- 5 7 .s
- swap dup .s
- rot .s
- * .s
- swap .s
- 1 + .s
- * .s
- . .s
|