Ana RamÃrez Chang
CS 302
Assignment 9 (groundwork and teams)
1. The following activities from assignment 4 would be appropriate for doing in a group.
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 endDraw the environment diagram that results from evaluating the following expression: sqrt 1.
2. Here are two activities that will faciliate group productivity:
3. I think a group exam problem should be involved enough for each student to have the opportunity to give input. So a simple recall type problem whould not work well for a group exam. The foldr question on my midterm is significantly involved that it would work well for a group problem on the midterm, especially if each student took the test individually first, that way each student would have thought about the solution beforehand and could discuss their solutions as a group to come up with a revised solution. Here is the foldr problem from my midterm.
Question 3: Use the following representation for a binary tree for this question.
datatype 'a tree = Tree of 'a * 'a tree * 'a tree | Empty
For example, the value:
var tr: int tree = Tree(3, Tree(2, Tree(1, Empty, Empty), Tree(7, Empty, Empty)), Empty)
represents the tree:
3 / 2 / \ 1 7
fun tree_flatten Tree(elt, Empty, Empty) = [elt]
| tree_flatten Tree(elt, Empty, rTree) = elt::(tree_flatten rTree)
| tree_flatten Tree(elt, lTree, Empty) = tree_flatten lTree) @ elt
| tree_flatten Tree(elt, lTree, rTree) =
tree_flatten lTree) @ (elt::(tree_flatten rTree))
Goal: Test understanding of simple recursion.
fun tree_flatten' Tree(elt, Empty, Empty) lst = elt::lst
| tree_flatten' Tree(elt, Empty, rTree) lst = elt::(tree_flatten' rTree lst)
| tree_flatten' Tree(elt, lTree, Empty) lst = tree_flatten' lTree elt
| tree_flatten' Tree(elt, lTree, rTree) lst =
tree_flatten' lTree elt::(tree_flatten' rTree lst)
Goal: Test ability to build a list without using "@", and ability to implement similar functionality in two ways (parts a. and b.)
fun tree_sum tr = let fun combfn(elt, sum) = elt + sum in treefold combfn 0 tr endImplement treefold so that it traverses the tree depth first from left to right (similar to the traversal in tree_flatten and tree_flatten').
fun treefold combfn base Tree(elt, Empty, Empty) = combfn(elt, base)
| treefold combfn base Tree(elt, lTree, Empty =
let
var foldedLTree = treefold combfn base lTree (optional blank)
in
combfn (elt, foldedLTree)
end
| treefold combfn base Tree(elt, Empty, rTree) =
let
var combinedElt = combfn(elt, base)(optional blank)
in
treefold combfn combinedElt rTree
end
| treefold combfn base Tree(elt, lTree, rTree) =
let
var foldedLTree = treefold combfn base lTree (optional blank)
var combinedElt = combfn(elt, foldedLTree)(optional blank)
in
treefold combfn combinedElt rTree
end
Goal: Test ability to transfer understanding of foldr to treefold.
fun tree_flatten tr =
let
fun combfn (elt, lst) = elt::lst
in
treefold combfn nil tr
end
Goal: Test ability to use treefold, including factoring out the combination function used in tree_flatten'.