Assignment 8: Abstraction
Due Date:Thursday March 9 2017, 12:00 PM
Important NoteWhenever we say to design a function, we mean that you need to follow the design recipe. Any other time that you write a function in this class, youalsoneed to follow the design recipe.
Important NoteUseIntermediate Studenton this assignment, and further assignments in this course.
1Map and Fold
Exercise 1Design a functionsmallestwhich accepts a posn and list of posn uses eitherfoldlorfoldrto find the posn clossest to the given posn. You should uselocalto define a local helper function.
Excercise 2Write a functioncountwhich accepts a list of numbers, and which uses either the built-in racketfoldlorfoldrfunctions, and counts the number of items in the list. You may NOT use the built inlengthfunction in this excercise. You will have to write an auxilary function which is given to the foldl or foldr functions which will perfom the counting.
Test the function with a few list of numbers.
Now abstract this function to work with a list of any data type.
2Book exercises
Exercises 238, 239, 241, 245
http://www.ccs.neu.edu/home/matthias/HtDP2e/part_three.html
3Abstracting trees
Here is the data definition for a binary tree of numbers. A leaf only contains a number, and a node contains a number, and one or two child elements.
;;ATreeisoneof:
;;(make-leafNumber)
;;(make-node1NumberTree)
;;(make-node2NumberTreeTree)
Exercise 3Design a functionsum-treethat adds up all the elements of aTree. Design another functionprod-treethe multiplies together all of the elements.
Exercise 4Abstractsum-treeandprod-treeinto a single functionop-tree. Useop-treeto re-definesum-treeandprod-tree.
Exercise 5Design the functioncount-treethat counts how manyleavesthere are in the tree.
Exercise 6Abstractsum-tree,prod-tree, andcount-treeinto a single functionprocess-tree. Is this the same as theop-treefunction you defined earlier? Again, redefine the previous functions using your new function.
Exercise 7GeneralizeTreeto[Tree X]which should support trees of any kind of data. Use your new definition to redefineTree.
Exercise 8Generalizeprocess-treeto work over[Tree X]. Do you need to change the body of the function?
Reviews
There are no reviews yet.