Evaluation and Application

We have now covered most of the key elements of the Scheme language, as well as some unique facets; however, all that is prelude. For now we will penetrate the Heart of the Mystery, the core pair of functions that make up the Scheme interpreter itself: (eval) and (apply).

The (eval) special form is a function which accepts a list and an optional environment variable (the default is the global environment), and - surprise! - evaluates the elements of the list in terms of that environment. It takes the first element of the list and if it is one of the primitive operations of the system, or if the identifier is bound to a function in the current environment, it calls (apply) with that function as the first argument and the rest of the list as the second.

The (apply) function in turn takes the passed function, and then (eval)s each of the elements in the list in turn, before using the returned values as the arguments for the function. To evaluate a non-primitive function, it would create a new environment for the function, take the list representling the program code and pass them both to (eval). The cycle continues until the expressions cannot be reduced any further.

This is the Scheme interpreter at it's most basic. The (eval)/(apply) cycle is the heart of every LISP type language. Everything else is just detail: if you can understand recursive evaluation, then you can understand Scheme.

Contents Previous Next