Wednesday, March 19, 2008

Eager vs. Lazy Evaulation

Programming languages can be categorized into eager or lazy evaluation. Most languages are in the eager evaluation.

But we can do somethings in C++ and Java to create lazy evaluation by constructing objects that store parameters for later evaluation.

class BigInteger;
class IntermediateSum {
const BigInteger &a, &b;
};

class BigInteger {
IntermediateSum operator+(const BigInteger &b) {
return new Intermediate(*this, b);
}
BigInteger& operator=(const IntermediateSum &i) {
/* now add */
}
};


The sum isn't evaluated until it hits the assignment. This lazy evaluation shows precisely when you can start evaluation and when you really need it.

No comments: