Ana Ramírez Chang
CS 302
Assignment 4

Recall:

  1. What is the type of foldr ?
  2. What is a stream?
  3. What is lazy evaluation?
  4. What does it mean to use data abstraction when writing code?
  5. What are the different parts of an environment diagram?

 

Comprehension, translation, interpretation:

  1. What is the difference between lazy evaluation and eager evaluation? Give an example when lazy evaluation would work better than eager evaluation.
  2. Give the type and value of the expression: if true then 4 else 7
  3. What are environment diagrams used for? What do they help you understand?
  4. Give two examples of data abstraction.
  5. What is the difference between a recursive function and a tail-recursive function?

 

Application to concrete situations:

  1. Given the expression:
      fun sqrt x = 
         let 
            fun good-enough guess = 
               Math.abs (guess * guess - x) < 0.001
            fun improve guess = 
               Math.average (guess, x/guess)
            fun sqrt-iter guess = 
               if good-enough guess then guess
               else sqrt-iter (improve guess)
         in
            sqrt-iter 1.0
         end
    	    
    Draw the environment diagram that results from evaluating the following expression: sqrt 1.
  2. Write a the function sum that takes a list of integers and returns the sum of all the integers using foldr.
  3. Write a datatype for family tree.
  4. Using the stream library from class, write a function that returns a stream of the fibonacci numbers (all of them).
  5. Implement map.

 

Analysis via recognizing relationships, discriminating elements, or picking out forms and patters:

  1. Write an expression that will have a different value depending on whether it is evaluated using lazy evaluation or eager evaluation.
  2. Write an expression that will have a different value depending on whether it is evaluated using static or dynamic scoping rules.
  3. Implement a tree using two different representations, but the same interface.
  4. What is the difference between mutable data and persistent data?
  5. What is the benefit of static scoping vs. dynamic scoping?

Synthesis that brings together elements of the material in new relationships or combinations:

  1. Write a function treefold, similar to foldr that takes a combination function and a tree.
  2. Write a function streamMap that takes a stream, and a function and returns a new stream with the function applied to each element of the stream.
  3. How can you sumulate lazy evaluation of function arguments in an eager interpreter?

Evaluation:

  1. Compare and contrast recursion and tail recursion from the perspective of engineering efficiency and runtime efficiency.
  2. Why is data abstraction useful. What could happen if you don't use data abstraction in your code? Under what circumstances is it most useful?
  3. What are the advantages of writing object-oriented code? If your language does not support object-oriented code what are some of "hacks" you could do to get some of these advantages?
  4. What are the benefits in terms of engineering efficiency of using a language with a static type system? Does it affect the runtime efficiency?
  5. Compare and contrast imperative and functional programming. Give examples of when each one is more appropriate.