Writing some logic in your first program
Factor handbook » Your first program

Prev:Creating a vocabulary for your first program
Next:Testing your first program


The Factor workflow is to edit source code on disk and then to refresh the live image. Let's examine the file that we just created with the scaffold tool.

Your palindrome.factor file should look like the following after the previous section:
! Copyright (C) 2022 Your name. ! See https://factorcode.org/license.txt for BSD license. USING: ; IN: palindrome

Notice that the file ends with an IN: form telling Factor that all definitions in this source file should go into the palindrome vocabulary using the IN: word. We will be adding new definitions after the IN: form.

In order to be able to call the words defined in the palindrome vocabulary, you need to issue the following command in the listener:
USE: palindrome

Now, we will be making some additions to the file. Since the file was loaded by the scaffold tool in the previous step, you need to tell Factor to reload it if it changes. Factor has a handy feature for this; pressing F2 in the listener window will reload any changed source files. You can also force a single vocabulary to reload, in case the refresh feature does not pick up changes from disk:
"palindrome" reload

We will now write our first word using :. This word will test if a string is a palindrome; it will take a string as input, and give back a boolean as output. We will call this word palindrome?, following a naming convention that words returning booleans have names ending with ?.

Recall that a string is a palindrome if it is spelled the same forwards or backwards; that is, if the string is equal to its reverse. We can express this in Factor as follows:
: palindrome? ( string -- ? ) dup reverse = ;

Place this definition at the end of your source file.

Now we have changed the source file, we must reload it into Factor so that we can test the new definition. To do this, simply go to the Factor listener and press F2. This will find any previously-loaded source files which have changed on disk, and reload them.

When you do this, you will get an error about the dup word not being found. This is because this word is part of the kernel vocabulary, but this vocabulary is not part of the source file's Parse-time word lookup. You must explicitly list dependencies in source files. This allows Factor to automatically load required vocabularies and makes larger programs easier to maintain.

To add the word to the search path, first convince yourself that this word is in the kernel vocabulary. Enter dup in the listener's input area, and press ⌃H. This will open the documentation browser tool, viewing the help for the dup word. One of the subheadings in the help article will mention the word's vocabulary.

Go back to the third line in your source file and change it to:
USING: kernel ;

Next, find out what vocabulary reverse lives in; type the word name reverse in the listener's input area, and press ⌃H.

It lives in the sequences vocabulary, so we add that to the search path:
USING: kernel sequences ;

Finally, check what vocabulary = lives in, and confirm that it's in the kernel vocabulary, which we've already added to the search path.

Now press F2 again, and the source file should reload without any errors. You can now go on and learn about Testing your first program.