CS 224 Lab 2 - Lazy Evaluation

Objectives

Perform the following tasks in the order given.

- Copy the lab2 directory.

- We will experiment a bit with lazy evaluation.
First consider the function f defined in Example2. This function has
an argument
*x*which is never used in the body of the function.

Try evaluating

f (1/0)

If the language was strict (uses eager evaluation) what result would you expect?

- Lazy evaluation gives us the benefit of allowing
"infinite" data structures. Look at the definitions of
*intsFrom*. This recursion is infinite, yet the following expression which takes the first 10 items from this list will halt, due to lazy evaluation. Only the portion of the list that is used will be computed. Try it out.

take 10 (intsFrom 1)

- Look at the definitions of
*fibs*and*primes*and see if you can figure out how they work

**Assignment:**Write the definition of the infinite list that computes all the powers of 2.

**Assignment:**Using the data type Tree, write a function*infTree*which creates an infinite tree where all the nodes contain the value given by the parameter.

infTree :: a -> Tree a

**Assignment:**Write the function*takeTree*which returns n levels of a Tree. Then use this function to print out a portion of an*infTree*.

takeTree :: Integer -> Tree a -> Tree a

- Send me your modified code in goucherLearn.