[an error occurred while processing this directive]

cs150: Notes 30

Schedule

Lazy Evaluation

What are the advantages of lazy evaluation?





What are the disadvantages of lazy evaluation?





Give an example of an expression that evaluates with both eager and lazy evaluation, but takes much longer to evaluate with eager evaluation.





What expressions take longer to evaluate with lazy evaluation than with eager evaluation?

class Thunk:
    def __init__(self, expr, env):
        self._expr = expr
        self._env = env
        self._evaluated = False
    def value(self):
        if not self._evaluated:
            self._value = forceeval(self._expr, self._env)
            self._evaluated = True
        return self._value
def forceeval(expr, env):
    value = meval(expr, env)
    if isinstance(value, Thunk):
        return value.value()
    else:
        return value

Eager Application Rule

def evalApplication(expr, env):
   subexprvals = map (lambda sexpr: meval(sexpr, env), expr)
   return mapply(subexprvals[0], subexprvals[1:])

Lazy Application Rule

def evalApplication(expr, env):

    ops = map (lambda sexpr: _______________________, expr[1:])

    return mapply(_________(expr[0], env), ops) 
Like most of my generation, I was brought up on the saying: 'Satan finds some mischief for idle hands to do.' Being a highly virtuous child, I believed all that I was told, and acquired a conscience which has kept me working hard down to the present moment. But although my conscience has controlled my actions, my opinions have undergone a revolution. I think that there is far too much work done in the world, that immense harm is caused by the belief that work is virtuous, and that what needs to be preached in modern industrial countries is quite different from what always has been preached. Everyone knows the story of the traveler in Naples who saw twelve beggars lying in the sun (it was before the days of Mussolini), and offered a lira to the laziest of them. Eleven of them jumped up to claim it, so he gave it to the twelfth. this traveler was on the right lines. But in countries which do not enjoy Mediterranean sunshine idleness is more difficult, and a great public propaganda will be required to inaugurate it. I hope that, after reading the following pages, the leaders of the YMCA will start a campaign to induce good young men to do nothing. If so, I shall not have lived in vain. ...

All this is only preliminary. I want to say, in all seriousness, that a great deal of harm is being done in the modern world by belief in the virtuousness of work, and that the road to happiness and prosperity lies in an organized diminution of work.

First of all: what is work? Work is of two kinds: first, altering the position of matter at or near the earth's surface relatively to other such matter; second, telling other people to do so. The first kind is unpleasant and ill paid; the second is pleasant and highly paid. The second kind is capable of indefinite extension: there are not only those who give orders, but those who give advice as to what orders should be given. Usually two opposite kinds of advice are given simultaneously by two organized bodies of men; this is called politics.

From In Praise of Idleness, by Bertrand Russell (co-author of Principia Mathematica), 1932


cs1120: Computer Science
University of Virginia
weimer@virginia.edu
Using these Materials