CS 224 Lab 2 - Lazy Evaluation

Objectives

  • Use lazy evaluation
  • Write infinite data structures
  • Perform the following tasks in the order given.

    1. Copy the lab2 directory.
       
    2. 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?
       
    3. 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)
       
    4. Look at the definitions of fibs and primes and see if you can figure out how they work
       

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


       


    6.  
      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
       


       


    7.  
      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
       

       

       
    8. Send me your modified code in goucherLearn.