Functional programming is easiest to explain in terms of what it isn't. In most languages, including Meta, programs are divided up into procedures (sometimes called methods), which are essentially little programs, and so are also called subprograms or subroutines. When you call (i.e. run) a procedure, you generally provide it with inputs (aka parameters or arguments) and it generates an output value in response (aka a result or return value). Some procedures also modify the computer in one way or another. These effects or side-effects of calling the procedure might include opening or closing a window, creating or deleting a file, or changing the value of a variable. All these operations that change the internal state of the computer are called imperatives; programming that uses imperatives is, not surprisingly, called imperative programming. Functional programming simply means programming without imperatives. In functional programming, procedure calls only generate outputs; they don't have other side-effects. Since those outputs are (usually) determined solely by the procedure's inputs, functional programs are a lot easier to reason about than imperative programs because when you're trying to predict that a procedure call will do, you only need to know what its inputs are; you don't need to think about what imperatives might have run before it, what order they were called in, and how they might affect the call you're thinking about. Or at least that's the theory. Of course, in practice any style of programming can be difficult. Moreover certain types of programs are better suited to certain types of styles of programming. Nevertheless, understanding functional programming is a good place to start learning programming. The basics of MetaA program in Meta consists of a sequence of expressions. Syntactically (syntax just means grammar), an expression in Meta is either - A name of a data object, either:
- A constant name (e.g. the characters "1.5" are the name of the number one-and-a-half), or
- A variable name, such as fred, this-is-a-name, or +.
Basically any series of characters that doesn't look like a constant and doesn't have spaces or brackets can be a variable name.
- A series of expressions enclosed in brackets, such as [+ 1 1] or [box 10 10].
That's the grammar of Meta. The meaning of expressions is as (roughly) follows. Each expression, when run, produces a value as its output. The output of an expression is determined by the following rules: - The value of a name is the data object represented by the name
- For constants, the value is determined solely by the spelling of the constant and can't be changed by the programmer.
- Number constants follow the basic rules used in arabic numbering: 1 means one, 10 means ten, and 0.5 means a half.
- Sequences of characters enclosed in double-quotes denote strings (i.e. data objects that represent chunks of text).
- There are also three magic names for specific constants
- true and false mean the Boolean data objects true and false.
- null means "the null value", which means no data object at all
- For variable names, their value is determined by their definition in a dictionary (aka the environment).
There are a number of predefined names in the dictionary and the programmer can define others.
- A bracketed expression is either
- One of a small number of special cases (see below), or otherwise
- A procedure call: [proc arg1 arg2 ... argn]. The system finds the value of this expression by first finding the values of all the subexpressions within the brackets (i.e. proc and arg1 through argn) and then runs the value of the first expression (the procedure), passing it the values of the rest of the expressions as inputs. The result of the call expression is then the output of the procedure.
Those rules account for the vast majority of the code in a Meta program. However, there are a few special-case bracketed expressions, also called special forms. With the exception of the → expression, these are all determined by the first word after the opening bracket: - [define name expression]
Tells Meta to find the value of expression and add an entry to the dictionary defining name to mean the expression's value.
- [if test consequent alternative]
Tells Meta to find the value of test and then return either the value of consequent, if the value of test was true, or the value of alternative, if the value of test was false.
- [cond [test1 value1] [test2 value2] ... [else elsevalue]]
Tells MEta to try each of the tests until it finds one that's true, at which point it returns its associated value. If none of them are true, it returns the elsevalue. It's equivalent to a series of nested if's: [if test1 value1 [if test2 value2 ... [if lasttest lastvalue elsevalue]]].
- [with name = expression result-expression]
Returns the value of result-expression, using the value of expression as the value for the variable name.
In other words, Meta starts by finding the value of expression, then make a temporary entry in the dictionary saying name means that value. It then finds the value of result-expression using this updated dictionary and returns its value. It also sets the dictionary back to its original form.
Note: you can include as many names (and values) as you want in a with expression, as in:
[with name1 = expression1
name2 = expression2 name3 = expression3
result-expression]
- [name → result-expression]
[name1 name2 ... namen → result-expression] Makes a procedure as its result. The procedure, when called, outputs the value of result-expression using the values of the procedure's input as the value for name. Again, you can include multiple names, as in the second expression, in which case the procedure must be called with multiple inputs (as many inputs as there are names). Note that an → expression doesn't run anything the result-expression. It only makes a procedure that will run the result-expression if and when it's called.
If a bracketed expression starts with the names: define, if, cond, or with, or if it has a → inside it, then it's a special form. Otherwise, it's a procedure call. The Meta language Tutorials |