03-stack-manipulation.fth 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. \ drop elements from the stack silently using `drop`
  2. 1 .s drop .s
  3. \ duplicate top element using `dup`
  4. 1 .s dup .s drop drop .s
  5. \ copy second element from top to the top of the stack
  6. 1 2 4 over .s
  7. \ swap first 2 elements from the top of the stack using `swap`
  8. 1 2 .s swap .s drop drop .s
  9. \ left shift first 3 elements on the stack. rotate first 3 elements on
  10. \ the stack so that lowest will be on top.
  11. 1 2 3 rot . . . .s
  12. \ swap and drop more
  13. 1 2 3 4 .s 2swap .s 2drop 2drop
  14. \ --> <4> 1 2 3 4 <4> 3 4 1 2 ok
  15. \ remove second element from top of the stack using `nip`
  16. 1 2 3 nip .s . . .s
  17. \ equivalent to
  18. 1 2 3 swap drop .s . . .s
  19. \ nip more
  20. 1 2 3 4 2nip .s . . .s
  21. \ copy top element and then swap it with the second element
  22. 1 2 3 4 tuck .s
  23. \ equivalent to
  24. 1 2 3 4 swap over .s
  25. \ 1 2 3 --> 3 2 1
  26. 1 2 3 swap rot .s . . .
  27. \ 1 2 3 --> 1 2 3 2
  28. 1 2 3 swap tuck .s . . . .
  29. \ 1 2 3 --> 1 2 3 3
  30. 1 2 3 dup .s . . . .
  31. \ 1 2 3 --> 1 3 3
  32. 1 2 3 nip dup .s . . .
  33. \ 1 2 3 --> 2 1 3
  34. 1 2 3 rot swap .s . . .
  35. \ 1 2 3 4 --> 4 3 2 1
  36. \ 2swap affects a range of 4 values, bringing values at position 3 and
  37. \ 4 to the front.
  38. 1 2 3 4 swap 2swap swap .s . . . .
  39. \ 1 2 3 --> 1 2 3 1 2 3
  40. 1 2 3
  41. rot \ get the 1 to the top
  42. dup \ copy the 1
  43. 2swap \ move the 1 back to the beginning
  44. \ --> 1 1 2 3
  45. rot rot \ get the 2 to the top
  46. dup \ copy the 2
  47. 2swap \ put the 2 back in place at the 2. position from the bottom
  48. rot rot \ correct rotation state
  49. \ --> 1 2 1 2 3
  50. dup \ copy the 3
  51. 2swap \ put the 3 in its place
  52. rot \ correct rotation state
  53. .s
  54. . . . . . .
  55. .s
  56. \ This approach is using the fact, that we can reach 4 elements wide
  57. \ using `2swap` and `rot` only affects the first 3 positions. Anything
  58. \ we move to the 4. position will remain untouched by `rot`. It is
  59. \ basically "stored".
  60. \ 1 2 3 4 --> 1 2 3 4 1 2
  61. \ + We cannot remove anything, because all elements are unique and we
  62. \ want to remain general.
  63. \ + We cannot add anything, while `1` is not at the lowest position,
  64. \ because we only have range 4 effects with our word.
  65. \ + We need to add 1 and 2 while 1 is at the lowest position. We have
  66. \ no word for this.
  67. \ + Unsure how to solve this.
  68. \ + Here is a cheap shot "solution":
  69. 1 2 3 4
  70. 1 2 .s . . . . . .
  71. \ 1 2 3 -->
  72. 1 2 3 . . . .s
  73. \ 1 2 3 --> 1 2 3 4
  74. 1 2 3 drop .s . . .
  75. \ 1 2 3 --> 1 3
  76. 1 2 3 nip .s . .
  77. \ combine with artihmetic words
  78. 5 dup * .
  79. \ Exercise: Write 17^3 and 17^4 in Forth, without writing 17 more than
  80. \ once.
  81. 17 dup dup * * .s .
  82. 17 dup dup dup * * .s .
  83. \ Write a piece of Forth code that expects two numbers on the stack (a
  84. \ and b, with b on top) and computes (a-b)(a+1).
  85. 5 7 .s
  86. swap dup .s
  87. rot .s
  88. * .s
  89. swap .s
  90. 1 + .s
  91. * .s
  92. . .s