Stack Words
To get anything done in Smojo, you need to use its stack. There are a few built-in words you can use:
Word | Action |
---|---|
DROP |
Pops the top item off the stack and discards it. This item is lost and can't be retrieved. |
DUP |
Duplicates the top of the stack. For example, 1 DUPends up with 1 1on the stack. Note that DUPdoes not re-create things on the stack; it merely duplicates pointers to things. |
SWAP |
Swaps the top two items on the stack. For example, "Hello" "World" SWAPends up with "World" "Hello"on the stack. |
Quiz
Question 1
Write a word calledDOUBLEthat puts double the number on the top of the stack. For example,
23 double .should display 46
: double dup +. ;
Question 2
Write a word called1/that puts reciprocal of the number on the top of the stack. For example,
5 1/ .should display 0.2
: 1/ 1 swap /. ;