Send Close Add comments: (status displays here)
Got it!  This site "creationpie.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Eager and lazy evaluation
by RS  admin@creationpie.com : 1024 x 640


1. Eager and lazy evaluation
The best way is often to delay decisions until they have to be made. In functional language terms, this is called lazy evaluation. The lazy method waits until it is known that something will be needed to work on it. The eager method keeps busy working on things that might never be needed.

2. Lazy methods
Book: Delphi Component DesignLazy methods will tend to defer costs until they are actually needed.

The advantage of cost deferral is that if you defer the cost long enough, you might not have to pay that cost at all. Thorpe, D. Delphi component design. Reading, MA: Addison-Wesley., p. 255.

Information sign More: Danny Thorpe

3. Eager evaluation
Things that you have to do, you might as well do them early when you have time to do them. In functional language terms, this is called eager evaluation.

Good decision makers know how to delay decisions until the need to be made without delaying the decision too long.

4. Studying for a test
Example: Studying for a test and then taking the test. Which method do you use?

5. Lazy/eager evaluation
Example: When you wear shoes with shoelaces, do you untie your shoelaces before taking your shoes off? Which method do you use? In the Army, one always waits until you see if they really want you to do what they asked you to do.

That is what employees often do, too. They wait to see if you really wanted what you said that you wanted.

Students do the same thing. Most people do.

6. Evaluation methods
An important concept in making decisions that involve getting information into the computer is that of eager versus lazy evaluation.

7. Eager evaluation
Eager evaluation evaluates everything at the earliest possible moment. So, all data would be computerized before the system would be used. The advantage is that eager evaluation is that it is often more efficient if it can be determined that the evaluation will, in fact, be required. For example, an eager evaluation scheme for relating zip codes to cities and states would be to get an entire list of zip codes related to cities and states.

8. Lazy evaluation
Lazy evaluation delays evaluation until it is required. So, data would be computerized as needed. If the data is never needed, it is never computerized. The advantage is that no more work is done than necessary.

A lazy evaluation scheme for relating zip codes to cities and states could be expressed using the following pseudo code algorithm. Notice how such a method will incrementally provide the relations needed. If a relation is never needed, no time or effort is expended obtaining and processing that information. As another example, consider a pizza shop that delivers. Even if a database containing the phone numbers and addresses of everyone in the county were available and up-to-date, it might be better to use a lazy method to obtain such information, adding phone numbers and addresses, as well as useful comments about the customer, when a customer places an order over the phone. If there are a significant number of repeat customers, as one would hope there would be, savings would be realized starting with the second call. In this case, a primary key for this example might be the phone number. And, if Caller-ID is available, a link to the computer might even obviate the need to ask the customer for their phone number. As a practical matter, the database could be purged of customers who have not ordered for more than a specified amount of time (say, one or two years).

As another example, instead of computerizing an entire manual of parts for a piece of equipment, which would involve a significant amount of effort, only ordered parts could be computerized, both to track the status of the parts and assuming that a payoff may occur if the same part is ordered more than once. Aside: The title of the author's Ph.D. dissertation was Issues in the implementation of lazy functional languages (1990), which was intimately involved with the ideas of copy-update problems, sharing properties, and eager and lazy evaluation in both sequential and parallel modes of execution.

9. End of page

by RS  admin@creationpie.com : 1024 x 640