14-recursion.fth 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 1 0 / . \ "Floating-point unidentified fault" in Gforth on some platforms
  2. \ Change `/` to throw an expection, if the divisor is 0,
  3. \ otherwise continue with normal division.
  4. : / ( n1 n2 -- n )
  5. dup 0= if
  6. \ report division by zero
  7. -10 throw
  8. endif
  9. \ Use original `/` version.
  10. /
  11. ;
  12. 1 0 /
  13. \ Here is a recursive implementation of factorial using
  14. \ `recurse`:
  15. : factorial ( n -- n! )
  16. dup 0> if
  17. \ Create a new factor, reduced by 1.
  18. dup 1-
  19. recurse
  20. \ When the calls return, do a multiplication for each
  21. \ iteration.
  22. *
  23. \ If the number on top of the stack is zero, drop it.
  24. else
  25. drop 1
  26. then ;
  27. \ Apparently `recurse` works simply by calling the function
  28. \ again and one has to make sure, that the stack is in the
  29. \ correct state, so that recurring works.
  30. 8 factorial .
  31. \ However, unfortunately there can be a stack overflow, if
  32. \ the recursion is too deep:
  33. 1000 factorial .
  34. \ Also unfortunately integer numbers overflow between 20!
  35. \ and 21!:
  36. 20 factorial .
  37. 20 factorial . 2432902008176640000 ok
  38. 21 factorial .
  39. 21 factorial . -4249290049419214848 ok
  40. \ Assignment: Implement Fibonacci using recursion.
  41. \ Note: This is a naive implementation.
  42. : fibonacci ( n -- n )
  43. \ The nth part of the calculation is not the number n
  44. \ itself. For example 1 1 2 3 5 8 ... the 8 is not the 8th
  45. \ Fibonacci number.
  46. \ Fibonacci of 2 an 1 is 1:
  47. \ 1 1 2 3 5 8 ...
  48. \ ^ ^
  49. \ If n is still greater than 2, we use the usual formula
  50. \ for recursively calculating Fibonacci numbers.
  51. dup 2 >
  52. if
  53. \ Put the number reduced by 1 on the stack and put the
  54. \ number reduced by 2 on the stack. The order does not
  55. \ really matter, since they will only be the basis for
  56. \ recursive calls and the results of those recursive
  57. \ calls will be added, and order does not matter for
  58. \ addition of integers.
  59. dup 1 -
  60. swap
  61. 2 -
  62. \ Calculate for n-2.
  63. recurse
  64. \ Swap the results to the 2. position on the stack, so
  65. \ that n-1 can be used for another recursive call.
  66. swap
  67. \ Calculate for n-1.
  68. recurse
  69. \ Add them both up.
  70. +
  71. \ Otherwise we hit the base case, which is returning 1.
  72. else
  73. \ Replace numbers, which are not > 2 with 1.
  74. drop 1
  75. then
  76. ;