REBOL Functions are not Commands
From time to time I hear REBOL users refer to the default functional words of REBOL as "commands". A programmer might refer to the "if command" or "while command". That terminology is incorrect, and it is good for beginners to understand why. In a nutshell, functions are not commands. This may seem like a subtle unimportant point, but in fact, understanding the difference is critical to understanding the REBOL language. In computer languages, a command refers to an operation that is interpreted by parsing expressions and matching a given input to symbols found in a table (the command names). The symbols themselves have no values. The method simply matches an input word with one found in the table. (Like the REBOL parse function, see note below.) A function is a mathematical construct that refers to not only the concept of evaluation (computation), but also the idea of flow. In REBOL, most expressions are created as a series of functions that are evaluated mathematically and produce a flow of results. For example the expression: insert clear find string "here" "where" evaluates the functions insert, clear, and find with data flowing between them. The evaluation of functions is not like that of commands. Functions do not require parsing with a table of symbols to determine their meaning. (In fact, a function does not even require a symbol at all -- but we won't go into that point here to avoid confusion.) When a REBOL expression is evaluated, it is not done by matching the word to a table of symbols. Instead, the words themselves have values -- those values being the functions to be used. This is why in REBOL you can write expressions like these: >> append "Test" 1 == "Test1" >> ap: get 'append >> ap "Test" 1 == "Test1" >> ap: :append >> ap "Test" 1 == "Test1" >> function? :append == true >> function? :ap == true >> :append = :ap == true This works because the word append refers to a value that is a function. The word append is not a command. It is variable that has a value. That value is a function. And, like any other value in REBOL, other words can refer to that value (as ap does in this example). Another example can be seen when you use the if function: result: if num > 10 [random 100] In most languages you would not normally think of if returning a result, because if would be a command. But, in REBOL if is a function. It returns a result just like other functions. This point is stressed even further when we make the statement that REBOL contains no keywords. REBOL is not a language built from a symbol table and rules that parse those symbols. REBOL is much more dynamic than that. It is built from a set of words that have values within specific contexts, and the words are allowed to take on different meanings in different contexts. Also note that in many languages, sequences of commands are referred to as statements. In REBOL, we do not have statements that are executed, we have expressions that we evaluate. That may seem like language snob talk, but it is important. The way we think about language greatly affects the way we use language. This functional approach is not something REBOL invented. The technique has been around for a long time, and languages that use it are often referred to as functional languages. REBOL falls into that class, but we don't make a big deal about it. REBOL is not considered a pure functional language (because in REBOL functions are allowed to have side effects -- we'll not get into that topic right now). Pure functional languages have their own special problems. So, does REBOL have commands at all? It turns out that it does. Dialects in REBOL are normally command-based languages, not functional languages. The parse function dialect is in fact a good example of that (but so is the secure dialect and the others). So, if you write: parse page [ some [ thru <title> copy text to </title> ] ] The words some, thru, copy, and to are not functions, they are more like commands. The parse function scans the block looking for words found in its special symbol table. So, it is ok to refer to some or thru as commands in this context. Just don't refer to parse as a command. It is a function. Keep that in mind, and you will be one step closer to becoming fluent in the REBOL language.
|
Updated 18-Nov-2024 - Copyright Carl Sassenrath - WWW.REBOL.COM - Edit - Blogger Source Code |