2019-03-18 18:58:32 +01:00
|
|
|
# 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:
|
|
|
|
|
|
|
|
```
|
2019-07-22 23:03:57 +02:00
|
|
|
:palindrome? dup s:reverse s:eq? ;
|
2019-03-18 18:58:32 +01:00
|
|
|
```
|
|
|
|
|
2021-03-30 13:58:25 +02:00
|
|
|
Naming uses the `:` sigil to add a new word to the dictionary.
|
2019-03-18 18:58:32 +01:00
|
|
|
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.
|