

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Due: TBD. Watch the blog for details. Problem 1: Define the following Scheme functions: A. foldl op init l - Takes a list of numbers, l, and uses op to accumulate their values starting with init. Specifically: (foldl + 0 (1 2 3)) = (+ (+ (+ 0 1) 2) 3) B. foldr op init l - Same as A, but evaluates from right to left. Specifically: (foldr + 0 (1 2 3)) = (+ 1 (+ 2 (+ 3 0))) C. foldl-tree - Same as foldl except that op is used to fold inner trees as well as the tree itself. D. parse t - Use foldl-tree to write a function that will check if a tree has the following form: o Each leaf node is an integer or a symbol o Each non-leaf node is the operation +,-,/ or * followed by other leaf and non-leaf nodes E. elaborate e t - Use foldl-tree to write a function that will perform the same function as part D, but will replace each symbol with the number associated with it in an association list passed as e. If the symbol is not found, it should be returned as the single return value.
Problem 2: Define a mechanism for using nested lists to represent n-ary trees. Use documentation in your code to define how a tree will be represented. Define the following collection of operations that use this representation for trees: A. tree-find – Accepts a tree and a predicate. Filter searches the tree depth first looking for a value satisfying the predicate and returns it. If no value is found, your function should return false. B. tree-flatten-pre-order – Walks the tree in pre-order and generates a list of its contents. (Note: this is a pre-order walk, not a sorting of the tree contents.) C. tree-flatten-post-order – Walks the tree like Part B except it generates a list of its contents in post-order. D. tree-map – Accepts a tree and a function. Walks the tree and generates a new tree formed by evaluating the function on each tree element. E. tree-flatten-in-order-map – Accepts a tree and a function. Walks the tree in order and generates a list of the results of applying the function to its contents. F. tree-assoc – Accepts a tree whose stored values are lists and an index value. The assoc function returns the stored value whose car is equal to the index value. You must use your tree-find function from Part A to implement this function. (Hint: The find function accepts a predicate and a tree. Obviously the tree passed to tree- assoc can be passed to find. However, you cannot pass the search value as the predicate. Herein lies the fun…) Problem 3: In class we talked about the general concept of currying where a function of two parameters can be turned into a function of one parameter. Recall the use of add to define an increment function by specifying a constant 1 for one of the add parameters: (define inc (lambda (x) (add x 1))) Let’s try to make the technique more general. Provide solutions for the following functions related to currying. A. tree-inc - Accepts a tree and adds one to every numeric value in it. Use currying and tree-map from the previous problem. B. curry-2 - Accepts a single function of two parameters and a value for the first parameter. Returns a function of one parameter that used as the value for the second parameter in the original function. (curry-2 add 1) returns a function that adds 1 to its argument. C. Redo Part A using Part B D. curry-3 - Accepts a single function of three parameters and value for the first parameter. Works the same as curry-2 except the result is a new function of 2 parameters. EECS 368 Programming Language Paradigms