Search code examples
schemelispevaluationsicpexpression-evaluation

Evaluate combination to tackle recursively (+ 1 2)


I am reading SICP's first chapter

1.1.3 Evaluating Combinations

It states that

To evaluate a combination, do the following: 

1.  Evaluate the subexpressions of the combination.

2.  Apply the procedure that is the value of the leftmost subexpression (the operator) to the arguments that are the values of the other subexpressions (the operands). 

Suppose an combination (+ 1 2)

According to the above algorithms,
The first is to evaluate (retrieve) + 1 and 2)
The second is to apply + to 1 (store the intermediate state)
The third is to evaluate(retrieve) the intermediate state and 2 again.
The forth is to apply the intermediate state to 2.

Is it right?
What' the intermediate state of +1?


Solution

  • The expression (+ 1 2) is a combination composed by the primitive expressions: +, 1, and 2.

    The first step says to evaluate all the subexpressions. In this case they are, respectively, the operator which performs the sum of numbers, and the numbers corresponding to the numerals 1 and 2, respectively.

    So you have an operator, and two numeric values. The second step says that you must apply the operator (leftmost value) to the two numbers: in other words you must apply the sum operator to 1 and 2, so to obtain the number 3. And the process terminate.

    Note that there are no intermediate states in this computation.