MaxiBarometer

FALSE fixed factorial

Nov 18th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. { Original code: }
  2. {
  3. [$1=~[$1-f;!*]?]f:
  4.  
  5. "calculate the factorial of [1..8]: "
  6. ß^ß'0-$$0>~8>|$
  7. "result: "
  8. ~[f;!.]?
  9. ["illegal input!"]?"
  10. "
  11. }
  12.  
  13. { New code: }
  14.  
  15. { factorial program in false! }
  16.  
  17. [$1=~[$1-f;!*]?]f: { fac() in false }
  18.  
  19. "calculate the factorial of [1..8]: "
  20. ß^ß'0-$$0>~\8>|$
  21. "result: "
  22. ~[\f;!.]?
  23. ["illegal input!"]?"
  24. "
  25.  
  26. {
  27. Explanation:
  28.  
  29. Let's say the input is 3.
  30.  
  31. code description stack after code
  32. ß^ß flush and read ['3']
  33. '0- convert character to number [3]
  34. $$ copy twice [3, 3, 3]
  35. 0 [3, 3, 3, 0]
  36. > compare against 0 [3, 3, true]
  37. ~ [3, 3, false]
  38. \ swap comparison result [3, false, 3]
  39. 8 [3, false, 3, 8]
  40. > [3, false, false]
  41. | or both conditions [3, false]
  42. $ copy for if/else [3, false, false]
  43.  
  44. ~ negate condition, so call function only if condition is false (so no input errors) [3, false, true]
  45. [\ swap top two elements. Remember, the "true" is gone because the "?" consumes it before this lambda is executed. [false, 3]
  46. f;!. call function and print result. There is no swap after this, since the function consumed the top element on the stack.
  47. ]? put lambda on the stack, and execute, since 3 is valid input [false]
  48.  
  49. ["illegal input!"]? use the second copy of the boolean for the error message []
  50.  
  51. }
Add Comment
Please, Sign In to add comment