13-loops.fth 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. \ `begin` marks the start of a loop.
  2. \ `again` jumps back to `begin` (goto style).
  3. : endless ( -- )
  4. 0 begin
  5. dup . 1+
  6. again ;
  7. endless
  8. \ At run-time `while` consumes a flag; if it is 0, execution
  9. \ continues behind the `repeat`; if the flag is non-zero,
  10. \ execution continues behind the `while`. `repeat` jumps
  11. \ back to begin, just like again.
  12. \ `2/` is right shift by 1, which is division by 2 of
  13. \ integers.
  14. : log2 ( +n1 -- n2 )
  15. \ logarithmus dualis of n1>0, rounded down to the next
  16. \ integer
  17. assert( dup 0> ) \ `assert(` is non-standard!
  18. 2/ 0 begin
  19. over 0> while
  20. 1+ swap 2/ swap
  21. repeat
  22. nip ;
  23. \ A version without `assert(`, simply without the check:
  24. : log2 ( +n1 -- n2 )
  25. \ divide by 2 and start with a counter of 0, which is 1
  26. \ less than the number of divisions already performed. For
  27. \ example 8 log2 should be 3, since 2³=8, but the division
  28. \ is possible 4 times, until we reach 0: 8 is 1000, so the
  29. \ right shifted intermediate results will be: 1000 -> 0100
  30. \ -> 0010 -> 0001 -> 0000, 4 shifts.
  31. 2/ 0
  32. \ So starting with 8 we have on the stack: 4 0. `begin`
  33. \ acts as a label we can jump to later.
  34. begin
  35. \ Copy the second cell. If the value is still greater
  36. \ than 0, continue with the loop. `while` will continue
  37. \ with the part that comes after `while`, if a -1 is on
  38. \ top of the stack. So the basic structure is:
  39. \ begin CONDITION while CONSEQUENCE repeat/again
  40. over 0> while
  41. \ We still got 4 0 on the stack in the first
  42. \ iteration. Calculate + 1, since we passed the
  43. \ condition once, so the number was divisable by 2. Then
  44. \ we have 4 1 on the stack.
  45. 1+
  46. \ The next thing we need to do is to right shift (divide
  47. \ by 2) again. But the counter is the first cell, so we
  48. \ need to swap it with the number, to be able to right
  49. \ shift the number, instead of the counter.
  50. swap
  51. \ Right shift, divide by 2.
  52. 2/
  53. \ Swap the numbers back again, so that the condition of
  54. \ `while` still works.
  55. swap
  56. \ Then repeat, until the condition becomes false.
  57. repeat
  58. \ We only want the counter to be left on the stack, so
  59. \ drop the number, which is at second cell.
  60. nip ;
  61. 7 log2 .
  62. 8 log2 .
  63. \ `until` consumes a flag; if it is non-zero, execution
  64. \ continues at the `begin`, otherwise after the `until`.
  65. : log2 ( +n1 -- n2 )
  66. \ Start with -1, so that we are again 1 less than the
  67. \ possible number of shifts until the number becomes zero.
  68. -1
  69. \ Begin the loop.
  70. begin
  71. \ Increase the counter. Initially to zero.
  72. 1+
  73. \ Right shift the number.
  74. swap 2/ swap
  75. \ Put the result of the <= 0 check to the top of the
  76. \ stack.
  77. over 0 <=
  78. \ Continue, if the condition is not yet met.
  79. until
  80. \ We only want the counter to be left on the stack, so
  81. \ drop the number, which is at second cell.
  82. nip ;
  83. \ Assignment: Write a definition for computing the greatest
  84. \ common divisor.
  85. : gcd ( +n1 +n2 -- +n3 )
  86. begin
  87. .s cr
  88. dup 0> while
  89. 2dup
  90. \ n1 n2 -> n1 n2 n1 n2
  91. mod
  92. \ n1 n2 n1 n2 -> n1 n2 remainder
  93. rot drop
  94. \ n2 remainder
  95. repeat
  96. drop ;
  97. \ counted loops
  98. : ^ ( n1 u -- n )
  99. \ n = the uth power of n1
  100. 1
  101. \ 1 is the neutral element of multiplication
  102. \ n1 u 1
  103. swap
  104. \ swapped to get the number for the power back to the top
  105. \ of the stack for future operations.
  106. \ n1 1 u
  107. 0
  108. \ put a 0 on top, so that `u+do` performs the correct
  109. \ number of iterations
  110. \ n1 1 u 0
  111. u+do
  112. \ how does `u+do` work?
  113. over *
  114. loop
  115. nip ;
  116. 3 2 ^ .
  117. 4 3 ^ .
  118. : fac ( u -- u! )
  119. 1
  120. \ 10 1
  121. swap
  122. \ 1 10
  123. 1+
  124. \ 1 11
  125. 1
  126. \ 1 11 1
  127. u+do .s cr
  128. \ (11 - 1 = 10 times the iteration)
  129. i
  130. .s cr
  131. *
  132. loop ;
  133. 5 fac .
  134. 7 fac .
  135. \ I found another way of looping in a counted way:
  136. : 10count ( -- )
  137. \ count to 10
  138. 10 0 ?DO
  139. i .
  140. LOOP ;
  141. \ Leave a loop at condition becoming true:
  142. : limited-count ( -- )
  143. 10 0 ?DO
  144. i \ put the counter on the stack
  145. dup . \ output the counter
  146. 3 = \ check if equal to 3
  147. IF LEAVE
  148. THEN LOOP ;
  149. \ Go in steps other than 1:
  150. : 10-stepped-count ( -- )
  151. \ count to 10
  152. 10 0 ?DO
  153. i .
  154. 2 +LOOP ;
  155. : 1024-stepped-count ( -- )
  156. \ count to 10
  157. 1025 0 ?DO
  158. i .
  159. i 0 = IF 1 ELSE i THEN
  160. .s cr
  161. +LOOP ;
  162. : counting-stepped-count ( -- )
  163. \ count to 10
  164. 0 \ starting point
  165. 10 0 ?DO
  166. i +
  167. .s cr \ output stack
  168. LOOP ;
  169. \ It seems loops can only be used inside definitions, which
  170. \ is a bit disappointing. Is there no way to use loops
  171. \ interactively in GForth?
  172. \ `u+do` is a GForth extension, which ensures, that the 2
  173. \ arguments on the stack are "correct" in the way, that
  174. \ upper limit is greater than lower limit. If one used `?DO`
  175. \ instead, it would run towards the maximum integer number,
  176. \ then wrap around to negative numbers and up to the "upper"
  177. \ limit again. `u+` stands for "unsigned positive".
  178. : 10count ( -- )
  179. \ count to 10
  180. 0 \ start at 0
  181. 10 0 \ upper lower
  182. u+do
  183. .s cr \ output stack
  184. 1 +
  185. loop
  186. drop ;
  187. \ Now try with incorrect arguments:
  188. : 10count ( -- )
  189. \ count to 10
  190. 0
  191. 0 10 \ lower upper
  192. u+do
  193. .s cr \ output stack
  194. 1 +
  195. loop
  196. drop ;
  197. \ This only prints `ok`. Try the same with `?do`. Should
  198. \ result in an almost endless loop.
  199. : 10count ( -- )
  200. \ count to 10
  201. 0
  202. 0 10 \ lower upper
  203. ?do
  204. .s cr \ output stack
  205. 1 +
  206. loop
  207. drop ;
  208. \ Maybe this facility could be used to implement a more
  209. \ elegant version?
  210. : timesdo ( n1 n2 -- )
  211. \ do something for n1 - n2 times
  212. ;
  213. \ But I have no idea how to make this word.
  214. \ Assignment: Write a definition for computing the nth
  215. \ Fibonacci number.
  216. : fib-step ( u1 u2 -- u2 u)
  217. \ assuming we have 2 subsequent fibonacci numbers on the
  218. \ stack, calculate the next one
  219. dup \ u1 u2 u2
  220. rot \ u2 u2 u1
  221. + \ u2 u3
  222. \ nip \ u3
  223. ;
  224. : neg? ( n1 -- f ) 0< ;
  225. : not ( f1 -- f ) 0= ;
  226. : fib ( u1 -- u )
  227. \ calculate the nth fibonacci number:
  228. \ 1 1 2 3 5 8 13 21 34 55.
  229. \ 10 -> 55
  230. dup neg? not
  231. if
  232. \ put lower limit on the stack, +2, because we put the
  233. \ first 2 elements of the series on the stack already.
  234. 0 2 +
  235. \ put series start on the stack
  236. 1 1 2swap
  237. \ 1 1 u1 2
  238. u+do
  239. fib-step
  240. loop
  241. nip
  242. else
  243. ." fib only works with positive integers"
  244. then ;