0% found this document useful (0 votes)
83 views6 pages

Assignment: 7: Due: Language Level: Allowed Recursion: Files To Submit: Warmup Exercises: Practise Exercises

This document provides instructions for Assignment 7 in CS 135 Winter 2012. It includes 4 questions to complete and submit solutions for in Racket files. Question 1 involves using an online evaluation tool to solve stepping problems. Question 2 asks to write a bst-remove function to remove nodes from binary search trees. Question 3 extends an arithmetic expression evaluator to handle defined constants. Question 4 writes an apply-in-order function to apply a list of functions to an input in succession. There is an optional 5% bonus question to define a contract for apply-in-order. The assignment is due March 14, 2011 and uses intermediate recursion and the listed files.

Uploaded by

Yanling Lin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views6 pages

Assignment: 7: Due: Language Level: Allowed Recursion: Files To Submit: Warmup Exercises: Practise Exercises

This document provides instructions for Assignment 7 in CS 135 Winter 2012. It includes 4 questions to complete and submit solutions for in Racket files. Question 1 involves using an online evaluation tool to solve stepping problems. Question 2 asks to write a bst-remove function to remove nodes from binary search trees. Question 3 extends an arithmetic expression evaluator to handle defined constants. Question 4 writes an apply-in-order function to apply a list of functions to an input in succession. There is an optional 5% bonus question to define a contract for apply-in-order. The assignment is due March 14, 2011 and uses intermediate recursion and the listed files.

Uploaded by

Yanling Lin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS 135 Winter 2012 Brad Lushman

Assignment: 7
Due: Language level: Allowed recursion: Files to submit:
Wed., March 14, 2011, 9:15am Intermediate Student with Lambda Structural, Generative, Accumulative bst-remove.rkt, apply-in-order.rkt, contract.rkt, eval-apply.rkt Warmup exercises: HtDP 17.3.1, 17.6.4, 19.1.5, 20.1.1, 20.1.2, 24.0.7, 24.0.8 Practise exercises: HtDP 17.6.5, 17.6.6, 19.1.6, 20.1.3, 21.2.3, 24.0.9

Here are the assignment questions you need to submit. 1. Perform the Assignment 7 questions using the online evaluation Stepping Problems tool linked to the course web page and available at https://www.student.cs.uwaterloo.ca/cs135/stepping/ The instructions are the same as A03 and A04; check there for more information, if necessary. Reminder: You should not use DrRackets Stepper to help you with this question, for a few reasons. First, as mentioned in class, DrRackets evaluation rules are slightly different from the ones presented in class; you are to use the ones presented in class. Second, in an exam situation, of course, you will not have the Stepper to help you. Third, you can re-enter steps as many times as necessary to get them correct, so you might as well maximize the educational benet. 2. For the purpose of presenting examples, we will use the following binary search tree throughout this question: (dene t (make-node 5 (make-node 3 (make-node 2 (make-node 1 empty empty) empty) (make-node 4 empty empty)) (make-node 7 (make-node 6 empty empty) empty))) On the last assignment, you wrote a function to add a new (key, value) pair to an existing binary search tree. Removing a node from a binary search tree is a somewhat harder problem, which you will explore here. There are several cases to consider: If the node to be removed has no children, then it can simply be removed. For example: (check-expect (bst-remove t 1) (make-node 5 (make-node 3 (make-node 2 empty empty) (make-node 4 empty empty)) (make-node 7 (make-node 6 empty empty) empty))) CS 135 Winter 2012 Assignment 7 1

If the node to be removed has exactly one child, we can replace it with its only child. For example: (check-expect (bst-remove t 7) (make-node 5 (make-node 3 (make-node 2 (make-node 1 empty empty) empty) (make-node 4 empty empty)) (make-node 6 empty empty))) If the node (with key k) to be removed has two children, we have to do more work. We must rst nd the nodes inorder successorthis is the node in the tree with the smallest key larger than k. We then replace the node with its inorder successor, and instead remove the inorder successor. Since the inorder successor will not have a left child (why?), we will be able to remove it according to one of the rst two cases. For example: (check-expect (bst-remove t 5) (make-node 6 (make-node 3 (make-node 2 (make-node 1 empty empty) empty) (make-node 4 empty empty)) (make-node 7 empty empty))) Note that the node whose key is 6 is the inorder successor of the node with key 5; hence the contents of the node with key 6 replace those of the node to be removed, and the original node with key 6 is removed. Keep in mind that not only the key, but also the value eld of the inorder successor must be copied into the node to be removed. Write the function bst-remove that consumes a BST and a key k, and produces the BST resulting from removing the node with key k from the given BST (if it exists). If there is no node with key k, in the tree, then bst-remove should produce the original BST unchanged. If you write any helper functions, you must declare them locally. Place your solution in the le bst-remove.rkt. 3. In this question, you will extend the eval/apply interpreter for general arithmetic expressions (from Slides 8-66 and 8-67) to accommodate dened constants. The data and structure denitions for general arithmetic expressions will now be as follows: (dene-struct ae (fn args)) ;; An arithmetic expression (AE) is one of ;; * a Num ;; * a Symbol (compliant with Scheme rules for identiers) ;; * (cons Symbol AEList) (where Symbol is either + or *) ;; ;; An AEList is one of ;; * empty ;; * (cons AE AEList) CS 135 Winter 2012 Assignment 7 2

(a) Write the functions eval1 and apply1, which differ from eval/apply as follows: eval1 consumes an arithmetic expression and an association list (where the key is Symbol and the value is Num), matching variable names to values. For example, (eval1 (+ x ( y 2)) ((x 2) (y 3) (z 4))) should produce the value 8. apply1 consumes a symbol (+ or ), a list of expressions, and an association list, and computes the number resulting from applying the function specied by the symbol to the expressions in the list. (b) Write the functions eval2 and apply2, which operate like eval1 and apply1, except that eval2 consumes a list of denes, ending with an expresion, as parameter. For example, (eval2 ((dene x 2) (dene y 3) (dene z 4) (+ x ( y 2)))) should produce the value 8. apply2 consumes a symbol (+ or ), a list of expressions, and an association list, and computes the number resulting from applying the function specied by the symbol to the expressions in the list. (Hint: Consider writing a (local!) helper function that uses an accumulator variable to store an association list of denes processed so far. As denes (e.g. (dene x 2)) are removed from the rst parameter, the corresponding pairs (e.g. (x 2)) are added to the second pair.) You may assume the following: No variable is dened more than once. All variables needed by the expression are dened. The denes only map variable names to literal numbers (so, for example, (dene x (+ 1 1)) would not be allowed). (c) 5% Bonus Write the functions eval3 and apply3 that differ from eval2 and apply2 in that the denes are now allowed to use expressions. For example, (eval3 ((dene x 2) (dene y (+ x 1)) (dene z (+ x x)) (+ x ( y 2)))) should produce the value 8. In this subquestion, you may not make the assumptions given in the previous subquestion. If a variable is dened more than once, or missing, or used before it is dened, your program should evaluate (error Bad denes.). All helper functions, other than lookup-al, must be declared locally. Place your solution in the le eval-apply.rkt. 4. In class, we have seen that we are now able to put functions into lists. What can we do with lists of functions? One thing is to apply each function in the list in succession to a given input. Write a function apply-in-order which consumes a list of functions (each of which consumes and produces a number) and a number. It should produce the results of applying the last function in the list to the given input, and then applying the second-last function to

CS 135 Winter 2012

Assignment 7

the output, and so on, until all functions in the list have been applied in succession. For example, (apply-in-order (list add1 sqr sub1) 5) yields 17 Note that the above list being passed to apply-in-order has three elements, each of which is a function that can takes a number as input, and produces a number as output. Place your solution in the le apply-in-order.rkt. This concludes the list of questions for which you need to submit solutions.

5. 5% Bonus: The function apply-in-order, which you wrote in question 3, would function properly in certain (but not all!) cases in which the functions in the list do not all consume and produce numbers. For example, (apply-in-order (list sqr rst) (4 5)) yields 16. Although apply-in-order can be used in this wider context, characterizing precisely when it can be expected to work, and formalizing these conditions as a contract, is tricky. For this question, you will come up with a data denition that can be used to build a contract that precisely species the kinds of inputs over which apply-in-order will work. Give this data denition and use it to build a correct, fully general contract for apply-in-order. Note: Although this question requires some amount of thinking outside the box, we are looking for a very precise solution that captures exactly the necessary conditions. Partial credit will not be given unless your solution is essentially correct with only minor errors. Your solution will almost certainly need a parameterized data denition, in the spirit of listof . Also note: For technical reasons, your data denition may assume that the list of functions is non-empty. Place your solution in the le contract.rkt.

Enhancements: Reminderenhancements are for your interest and are not to be handed in. The material below rst explores the implications of the fact that Scheme programs can be viewed as Scheme data, before reaching back seventy years to work which is at the root of both the Scheme language and of computer science itself. The text introduces structures as a gentle way to talk about aggregated data, but anything that can be done with structures can also be done with lists. Section 14.4 of HtDP introduces a representation of Scheme expressions using structures, so that the expression (+ ( 3 3) ( 4 4)) is represented as (make-add (make-mul 3 3) (make-mul 4 4))

CS 135 Winter 2012

Assignment 7

But, as discussed in lecture, we can just represent it as the hierarchical list (+ ( 3 3) ( 4 4)). Scheme even provides a built-in function eval which will interpret such a list as a Scheme expression and evaluate it. Thus a Scheme program can construct another Scheme program on the y, and run it. This is a very powerful (and consequently somewhat dangerous) technique. Sections 14.4 and 17.7 of HtDP give a bit of a hint as to how eval might work, but the development is more awkward because nested structures are not as exible as hierarchical lists. Here we will use the list representation of Scheme expressions instead. In lecture, we saw how to implement eval for expression trees, which only contain operators such as +,,,/ , and do not use constants. Continuing along this line of development, we consider the process of substituting a value for a constant in an expression. For instance, we might substitute the value 3 for x in the expression (+ ( x x) ( y y)) and get the expression (+ ( 3 3) ( y y)). Write the function subst which consumes a symbol (representing a constant), a number (representing its value), and the list representation of a Scheme expression. It should produce the resulting expression. Our next step is to handle function denitions. A function denition can also be represented as a hierarchical list, since it is just a Scheme expression. Write the function interpret-with-one-def which consumes the list representation of an argument (a Scheme expression) and the list representation of a function denition. It evaluates the argument, substitutes the value for the function parameter in the functions body, and then evaluates the resulting expression using recursion. This last step is necessary because the function being interpreted may itself be recursive. The next step would be to extend what you have done to the case of multiple function denitions and functions with multiple parameters. You can take this as far as you want; if you follow this path beyond what weve suggested, youll end up writing a complete interpreter for Scheme (what youve learned of it so far, that is) in Scheme. This is treated at length in Section 4 of the classic textbook Structure and Interpretation of Computer Programs, which you can read on the Web in its entirety at http://mitpress.mit.edu/sicp/ . So well stop making suggestions in this direction and turn to something completely different, namely one of the greatest ideas of computer science. Consider the following function denition, which doesnt correspond to any of our design recipes, but is nonetheless syntactically valid: (dene (eternity x) (eternity x)) Think about what happens when we try to evaluate (eternity 1) according to the semantics we learned for Scheme. The evaluation never terminates. If an evaluation does eventually stop (as is the case for every other evaluation you will see in this course), we say that it halts. The non-halting evaluation above can easily be detected, as there is no base case in the body of the function eternity. Sometimes non-halting evaluations are more subtle. Wed like to be able to write a function halting?, which consumes the list representation of the denition of a function with one CS 135 Winter 2012 Assignment 7 5

parameter, and something meant to be an argument for that function. It produces true if and only if the evaluation of that function with that argument halts. Of course, we want an application of halting? itself to always halt, for any arguments it is provided. This doesnt look easy, but in fact it is provably impossible. Suppose someone provided us with code for halting?. Consider the following function of one argument: (dene (diagonal x) (cond [(halting? x x) (eternity 1)] [else true])) What happens when we evaluate an application of diagonal to a list representation of its own denition? Show that if this evaluation halts, then we can show that halting? does not work correctly for all arguments. Show that if this evaluation does not halt, we can draw the same conclusion. As a result, there is no way to write correct code for halting?. This is the celebrated halting problem, which is often cited as the rst function proved (by Alan Turing in 1936) to be mathematically denable but uncomputable. However, while this is the simplest and most inuential proof of this type, and a major result in computer science, Turing learned after discovering it that a few months earlier someone else had shown another function to be uncomputable. That someone was Alonzo Church, about whom well hear more shortly. For a real challenge, denitively answer the question posed at the end of Exercise 20.1.3 of the text, with the interpretation that function=? consumes two lists representing the code for the two functions. This is the situation Church considered in his proof.

CS 135 Winter 2012

Assignment 7

You might also like