Go to the first, previous, next, last section, table of contents.


Defining New Muf Words

Suppose you find yourself joining three strings together frequently:

Stack: "String1" "String2" "String3"
join join
Stack: "String1String2String3"

You'd like to have a single word that does this, to save a little typing. (Later we'll see a built-in way of joining groups of strings.) Well, MUF may be simple-minded, but it does have an excellent memory, and is good at learning new tricks, provided we can explain them precisely. Let's teach MUF a new word join3 that joins three strings together by doing two joins:

Stack:
defineWord: join3   join join   ;
Stack:
"String1" "String2" "String3"
Stack: "String1" "String2" "String3"
join3
Stack: "String1String2String3"

What could be simpler?

Actually, programmers are much too lazy to type a word as long as "defineWord:" all the time, so MUF allows you to abbreviate this word to just ":" -- saving you eleven whole characters of typing on every word you define!

The general way to teach MUF a new word is:

Type a colon to tell MUF that you are defining a new word.
Type the name of the new word.
Type the words which MUF should do each time it sees the new word.
Type a semi-colon to tell MUF that you are finished defining the word.

Once you've defined a new word, it works just like the built-in words. In particular, you can use it to define more words. Let's define a join4 that uses our join3:

Stack:
: join4   join3 join   ;
Stack:
"String1" "String2" "String3" "String4"
Stack: "String1" "String2" "String3" "String4"
join4
Stack: "String1String2String3String4"

Now you know almost enough to follow the "Hello world!" word we defined when discussing the spirit of MUF:

: hw "Hello world!\n" , ;

Clearly, hw is the name of the word, the : and ; are just the markers for the start and end of the word definition, and "Hello world!\n" is just the string to be printed. (The "\n" makes MUF start a new line. This is a convention borrowed from C. The 'n' stands for "new line" and the '\\' tells MUF that the next letter is special.) That leaves only the comma unexplained, so it is not too hard to guess that comma is the MUF word for printing a string to the user's screen.

Let's try defining a word that prints several lines, to see if we have got the idea right:

Stack:
: print-4-lines "Line1!\n" , "Line2!\n" , "Line3!\n" , "Line4!\n" , ;
Stack:
print-4-lines
Line1!
Line2!
Line3!
Line4!
Stack:

Neat!

The alert reader may be wondering if the words we're defining become available to everyone using MUF on the system, and whether they will be there the next time you run MUF.

It would be an awful mess if every word defined by one user immediately appeared in the MUF run by every other user: Different users might wind up redefining each other's words and getting very confused when words suddenly appeared or disappeared or changed.

So unless you do something special, words you define are only visible to you. But they will be there for you next time you run MUF, unless you delete them. Later on, we'll show you how to make words available to other people when you want, and how to take advantage of nonstandard packages of words that other people may have made available for you.

Note: What we are calling 'words' (a traditional name in Forth circles, Forth being a distant ancestor of MUF) are called various other things in other programming languages, and even by other MUF programmers. You'll most frequently see them called "functions", "subroutines", "procedures" or "operators". These names all mean much the same thing, and we use the terms interchangably in the Muq MUF documentation. I usually call them 'functions', a term loosely derived from mathematics and popular among C programmers.


Go to the first, previous, next, last section, table of contents.