MATH 2220 Final: MATH 2220 Cornell Final Exam 1

119 views10 pages
31 Jan 2019
Department
Course
Professor

Document Summary

You have one and a half hours to do this exam. All programs in this exam must be written in sml. In class, we de ned a reduction operator as a commutative and as- sociative function, and showed how to write a function that applies a reduction operator to the elements of a list. Write a curried func- tion that takes a reduction operator, an initial value and a binary tree as input, and applies the reduction operator to the initial value and the elements of the binary tree. Your code must specify the type of this function. You may assume that the binary tree is built from the following datatype. datatype "a binarytree = | node of "a binarytree * "a binarytree * "a. For example, reduce (op+) 4 (node(leaf(1),leaf(2),3)) should evaluate to the value 10. Solution: fun reduce (f:"a*"b->"b) (z:"b) (t:"a tree) = case t of (leaf(n)) |(node(l,r,d)) => reduce f (f (d, reduce f z l)) r.