Ana RamÃrez Chang
CS 302
Assignment 4
Recall:
- What is the type of foldr ?
- What is a stream?
- What is lazy evaluation?
- What does it mean to use data abstraction when writing code?
- What are the different parts of an environment diagram?
Comprehension, translation, interpretation:
- What is the difference between lazy evaluation and eager evaluation? Give an example when lazy evaluation would work better than eager evaluation.
- Give the type and value of the expression: if true then 4 else 7
- What are environment diagrams used for? What do they help you understand?
- Give two examples of data abstraction.
- What is the difference between a recursive function and a tail-recursive function?
Application to concrete situations:
- 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.
- Write a the function sum that takes a list of integers and returns the sum of all the integers using foldr.
- Write a datatype for family tree.
- Using the stream library from class, write a function that returns a stream of the fibonacci numbers (all of them).
- Implement map.
Analysis via recognizing relationships, discriminating elements, or picking out forms and patters:
- Write an expression that will have a different value depending on whether it is evaluated using lazy evaluation or eager evaluation.
- Write an expression that will have a different value depending on whether it is evaluated using static or dynamic scoping rules.
- Implement a tree using two different representations, but the same interface.
- What is the difference between mutable data and persistent data?
- What is the benefit of static scoping vs. dynamic scoping?
Synthesis that brings together elements of the material in new relationships or combinations:
- Write a function treefold, similar to foldr that takes a combination function and a tree.
- 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.
- How can you sumulate lazy evaluation of function arguments in an eager interpreter?
Evaluation:
- Compare and contrast recursion and tail recursion from the perspective of engineering efficiency and runtime efficiency.
- 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?
- 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?
- What are the benefits in terms of engineering efficiency of using a language with a static type system? Does it affect the runtime efficiency?
- Compare and contrast imperative and functional programming. Give examples of when each one is more appropriate.