06-definitions.fth 567 B

1234567891011121314151617181920212223
  1. : square ( n -- n² ) dup * ;
  2. 5 square .
  3. 7 square .
  4. : cube ( n -- n³ ) dup square * ;
  5. 6 cube .
  6. 2 cube .
  7. : n^4 ( n -- n^4 ) square square ;
  8. 3 n^4 .
  9. 2 n^4 .
  10. \ Assignment: Write colon definitions for nip, tuck, negate,
  11. \ and /mod in terms of other Forth words, and check if they
  12. \ work (hint: test your tests on the originals first). Don't
  13. \ let the `redefined'-Messages spook you, they are just
  14. \ warnings.
  15. : nip ( a b c -- a b ) swap drop ;
  16. : tuck ( a b c -- a c b c ) swap over ;
  17. : negate ( n -- -n ) -1 * ;
  18. : /mod ( a b -- a modulo b, a div b) 2dup mod rot / ;