6.16.5 Procedures for On the Fly Evaluation

Scheme has the lovely property that its expressions may be represented as data. The eval procedure takes a Scheme datum and evaluates it as code.

Scheme Procedure: eval exp module_or_state
C Function: scm_eval (exp, module_or_state)

Evaluate exp, a list representing a Scheme expression, in the top-level environment specified by module_or_state. While exp is evaluated (using primitive-eval), module_or_state is made the current module. The current module is reset to its previous value when eval returns. XXX - dynamic states. Example: (eval ’(+ 1 2) (interaction-environment))

Scheme Procedure: interaction-environment
C Function: scm_interaction_environment ()

Return a specifier for the environment that contains implementation–defined bindings, typically a superset of those listed in the report. The intent is that this procedure will return the environment in which the implementation would evaluate expressions dynamically typed by the user.

See Environments, for other environments.

One does not always receive code as Scheme data, of course, and this is especially the case for Guile’s other language implementations (see Support for Other Languages). For the case in which all you have is a string, we have eval-string. There is a legacy version of this procedure in the default environment, but you really want the one from (ice-9 eval-string), so load it up:

(use-modules (ice-9 eval-string))
Scheme Procedure: eval-string string [#:module=#f] [#:file=#f] [#:line=#f] [#:column=#f] [#:lang=(current-language)] [#:compile?=#f]

Parse string according to the current language, normally Scheme. Evaluate or compile the expressions it contains, in order, returning the last expression.

If the module keyword argument is set, save a module excursion (see Module System Reflection) and set the current module to module before evaluation.

The file, line, and column keyword arguments can be used to indicate that the source string begins at a particular source location.

Finally, lang is a language, defaulting to the current language, and the expression is compiled if compile? is true or there is no evaluator for the given language.

C Function: scm_eval_string (string)
C Function: scm_eval_string_in_module (string, module)

These C bindings call eval-string from (ice-9 eval-string), evaluating within module or the current module.

C Function: SCM scm_c_eval_string (const char *string)

scm_eval_string, but taking a C string in locale encoding instead of an SCM.

Scheme Procedure: apply proc arg … arglst
C Function: scm_apply_0 (proc, arglst)
C Function: scm_apply_1 (proc, arg1, arglst)
C Function: scm_apply_2 (proc, arg1, arg2, arglst)
C Function: scm_apply_3 (proc, arg1, arg2, arg3, arglst)
C Function: scm_apply (proc, arg, rest)

Call proc with arguments arg … and the elements of the arglst list.

scm_apply takes parameters corresponding to a Scheme level (lambda (proc arg1 . rest) ...). So arg1 and all but the last element of the rest list make up arg …, and the last element of rest is the arglst list. Or if rest is the empty list SCM_EOL then there’s no arg …, and (arg1) is the arglst.

arglst is not modified, but the rest list passed to scm_apply is modified.

C Function: scm_call_0 (proc)
C Function: scm_call_1 (proc, arg1)
C Function: scm_call_2 (proc, arg1, arg2)
C Function: scm_call_3 (proc, arg1, arg2, arg3)
C Function: scm_call_4 (proc, arg1, arg2, arg3, arg4)
C Function: scm_call_5 (proc, arg1, arg2, arg3, arg4, arg5)
C Function: scm_call_6 (proc, arg1, arg2, arg3, arg4, arg5, arg6)
C Function: scm_call_7 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
C Function: scm_call_8 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
C Function: scm_call_9 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)

Call proc with the given arguments.

C Function: scm_call (proc, ...)

Call proc with any number of arguments. The argument list must be terminated by SCM_UNDEFINED. For example:

scm_call (scm_c_public_ref ("guile", "+"),
          scm_from_int (1),
          scm_from_int (2),
          SCM_UNDEFINED);
C Function: scm_call_n (proc, argv, nargs)

Call proc with the array of arguments argv, as a SCM*. The length of the arguments should be passed in nargs, as a size_t.

Scheme Procedure: primitive-eval exp
C Function: scm_primitive_eval (exp)

Evaluate exp in the top-level environment specified by the current module.