CS 224 Lab 2
- Lazy Evaluation
Use lazy evaluation
Write infinite data structures
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.
If the language was strict (uses eager evaluation) what result would you
- 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
definition of the infinite list that computes all the powers of 2.
data type Tree, write a function infTree which creates an
infinite tree where all the nodes contain the value given by the
infTree :: a -> Tree a
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 ->
- Send me your modified code in goucherLearn.