retroforth/doc/book/general/quick-tutorial
crc 6a4aaf8eac prefix: namespace is now sigil:, rename words, update examples, update docs
FossilOrigin-Name: 25cf19660ab7728d7bfee2722ea826a8a438faf92b2504b28d922d2958906aed
2021-03-30 11:58:25 +00:00

44 lines
1 KiB
Text

# A Quick Tutorial
Programming in RETRO is all about creating words to solve
the problem at hand. Words operate on data, which can be
kept in memory or on the stack.
Let's look at this by solving a small problem: writing a
word to determine if a string is a palindrome.
A palindrome is a phrase which reads the same backward
and forward.
We first need a string to look at. Starting with something
easy:
```
'anna
```
Looking in the Glossary, there is a `s:reverse` word for
reversing a string. We can find `dup` to copy a value, and
`s:eq?` to compare two strings. So testing:
```
'anna dup s:reverse s:eq?
```
This yields -1 (`TRUE`) as expected. So we can easily
name it:
```
:palindrome? dup s:reverse s:eq? ;
```
Naming uses the `:` sigil to add a new word to the dictionary.
The words that make up the definition are then placed, with a
final word (`;`) ending the definition. We can then use this:
```
'anna palindrome?
```
Once defined there is no difference between our new word and
any of the words already provided by the RETRO system.