# 11.3.3 Definitions and Names

Charme provides an if expression special form with a syntax and evaluation rule identical to the Scheme if expression. The grammar rule for an if expression is:

The expression object representing an if expression should be a list containing three elements, with the first element matching the keyword if.

All special forms have this property: they are represented by lists where the first element is a keyword that identifies the special form.

The is_special_form procedure takes an expression and a keyword and outputs a Boolean. The result is True if the expression is a special form matching the keyword:

def is_special_form(expr, keyword):
return isinstance(expr, list) and len(expr) > 0 and expr[0] == keyword

We can use this to recognize different special forms by passing in different keywords. We recognize an if expression by the if token at the beginning of the expression:

def is_if(expr):
return is_special_form(expr, 'if')

The evaluation rule for an if expression is:1

Charme Evaluation Rule 5: If. To evaluate an if expression in the current environment, (a) evaluate the predicate expression in the current environment; then, (b) if the value of the predicate expression is a false value then the value of the if expression is the value of the alternate expression in the current environment; otherwise, the value of the if expression is the value of the consequent expression in the current environment.

This procedure implements the if evaluation rule:

def eval_if(expr,env):
if meval(expr[1], env) != False: return meval(expr[2],env)
else: return meval(expr[3],env)

1. We number the Charme evaluation rules using the numbers we used for the analogous Scheme evaluation rules, but present them in a different order.