Lists, Lists, Lists
- Due: 11:00pm Thursday, 17 September
- Starter files:
- fork wellesleycs251 / cs251-lists, add bpw as admin
- Programming answers in
functions.rkt - Text answers in
answers.txt
- Submission: commit and push your completed work to your Bitbucket fork as in assignment zero (but with the repository for this assignment).
- Relevant reading:
- Computability and the Halting Problem
- Racket
- McCarthy, Recursive Functions of Symbolic Expressions and Their Computation by Machine, 1960. Sections 1-2 (pages 1-8). Other sections optional.
- Tools:
Notes
- This assignment will take longer than the last. Start early!
- The problems are not sorted by difficulty. Feel free to jump around.
- Inspiration: ‘(1 2 3 4
1. Racket Programming1 (70 points)
Please do not use DrRacket’s “box comments” in your code. They interact poorly with our grading infrastructure.
Complete the following programming problems. If at all possible, avoid the use of the built-in list append function (and the equivalent list-append we wrote in lecture today). One or two functions below may seem to require append or something equivalent—feel free to use append there, but note its running time is O(n) given an n-element first argument. Soon we will see a new style of recursion for an efficient, elegant alternative.
-
Write a function
mergethat merges two lists into one as in merge-sort. Assuming the two argument lists are each sorted lists of numbers, this function will return a sorted list containing all elements from the two lists. Your function does not need to work for inputs that are not sorted.> (merge (list 1 3 6 7 9 10) (list 2 4 5 8)) '(1 2 3 4 5 6 7 8 9 10) > (merge (list 4 5 6) (list 1 2 3)) '(1 2 3 4 5 6) > (merge null null) '() ; this means null -
Write a function
revthat takes a listxsand reverses its order. You may not use the built-inreversefunction.> (rev (list 1 (list 2 3) (list 4 5 (list 6 7 8)))) '((4 5 (6 7 8)) (2 3) 1) > (rev (list 1 2 3 4 5)) '(5 4 3 2 1) > (rev (list 1)) '(1) > (rev null) '() -
Write a function
deep-revthat takes any argumentxand deeply reverses it. Ifxis an atom,deep-revreturns it as is. Ifxis aconscell,deep-revassumes it is a list, reverses the list, and deeply reverse any lists that are elements inx. Assume thatconscells are used only to represent lists. Thecons?function returns#tif its argument is aconscell and#fotherwise.2> (deep-rev (list 1 (list 2 3) (list 4 5 (list 6 7 8)))) '(((8 7 6) 5 4) (3 2) 1) > (deep-rev (list 1 2 3 4 5)) '(5 4 3 2 1) > (deep-rev (list 1)) '(1) > (deep-rev null) '() -
Write a function
contains-multiplethat takes an integermand a list of integersnsthat returns#tifmevenly divides at least one element of the integer listns; otherwise it returns#f. Usemoduloto determine divisibility.> (contains-multiple 5 (list 8 10 14)) #t > (contains-multiple 3 (list 8 10 14)) #f > (contains-multiple 5 null) #f -
Write a function
all-contain-multiplethat takes an integernand a list of lists of integersnss(pronounced “enziz”) and returns#tif each list of integers innsscontains at least one integer that is a multiple ofn; otherwise it returns#f.> (all-contain-multiple 5 (list (list 17 10 2) (list 25) (list 3 7 5)) #t > (all-contain-multiple 3 (list (list 17 10 2) (list 25) (list 3 7 5)) #f > (all-contain-multiple 3 null) #t -
Write a function
bitsthat takes a natural numbernand returns a list of the bits (0s and1s) in the binary representation ofn.> (bits 5) '(1 0 1) > (bits 10) '(1 0 1 0) > (bits 11) '(1 0 1 1) > (bits 22) '(1 0 1 1 0) > (bits 23) '(1 0 1 1 1) > (bits 46) '(1 0 1 1 1 0) > (bits 1) '(1) > (bits 0) '()Hint: The above sequence of examples has been chosen carefully to illustrate the divide/conquer/glue nature of the solution.
Optional 240-tinted challenge: Write a separate function
bits-2sthat takes a natural numberband an integernand a returns a list of0s and1s representing all digits of theb-bit two’s-complement representation of integern.> (bits-2s 4 6) '(0 1 1 0) > (bits-2s 4 -6) '(1 0 1 0) -
Write a function
censor-wordthat takes a stringwand returnswifwis not is a word in the bad words list'(algorithms midterm extension databases systems grade)and returns'XXXXifwis a bad word. The built-inmemberfunction takes a valuexand a listxsand returns#fifxis not an element inxsor non-#fotherwise. We have been avoiding writing the quote notation in Racket programs so far. Make an exception here to use symbols. We will discuss these more later.> (censor-word 'apple) 'apple > (censor-word 'midterm) 'XXXXWrite a second function
censorthat usescensor-wordand the built-inmapfunction to censor sentences by replacing all bad words:censortakes a list of strings and returns a list of strings with bad words replaced by'XXXX. Do not use recursion incensor.2> (censor '(I need an extension because I have an algorithms midterm)) '(I need an XXXX because I have an XXXX XXXX) > (censor '(Programming languages is more fun than systems)) '(Programming languages is more fun than XXXX)
2. Error Detection Constructs2 (10 points)
Evaluation of a Racket expression can either terminate normally (and return a value), terminate abnormally with an error, or run forever. Some examples of expressions that terminate with an error are (/ 3 0), division by 0; (car 2), taking the car of an atom; and (+ 3 #f), adding a boolean to a number. As we have seen in evaluation rules for Racket expressions, dynamic type-checking detects these errors. In DrRacket, this terminates evaluation and prints a message to the screen. Suppose that you work at a software company that builds text editors in Racket-with-Side-Effects. (It’s been done in a very similar language: Emacs is built in a dialect of Lisp!) Side effects are observable changes that are not just values returned by evaluation. They include: changing the value stored in a mutable variable (like variables work in Java), sending data across the network, displaying text or images on a screen, and countless other operations. The subset of Racket we have studied so far lacks side effects, but the full language includes operations with side effects.
Your boss, who is notoriously skittish about side effects, wants to handle errors in Racket programs without terminating the computation, but doesn’t know how. Thus, the job has fallen to you (the programming language expert).
-
Your boss asks you to implement a Racket construct
(error? e)that detects whether an expressionewill cause an error when evaluated. More specifically, your boss wants evaluation of(error? e)to return the value#tif evaluatingeterminates with an error and return the value#fif evaluatingebehave in any way except terminating in error. Explain why it is not possible to add theerror?construct to Racket. -
Your boss asks you to implement a Racket construct
(guarded e)that either evaluateseand returns its value, or ifewould halt with an error, returns0without performing any side effects. This could be used to try to evaluateeand if an error would occur, just use0instead. For example,(+ (guarded e1) e2) ; just (+ 0 e2) if e1 halts with an error ; (+ e1 e2) otherwisewill have the value of
e2if evaluation ofe1would halt in error, and the value of(+ e1 e2)otherwise. Describe how you might implement theguardedconstruct. What difficulties might you encounter? Notice that unlike(error? e), evaluation of(guarded e)does not need to finish and produce a result in all cases.
3. Conditional Expressions (20 points)
We introduced the if expression in Racket. The original definition of Lisp focused instead on a related cond expression, presented by McCarthy in his 1960 paper, Recursive Functions of Symbolic Expressions and Their Computation by Machine, which discussed foundations and implementation details of Lisp. The name cond stands for conditional. Read sections 1-2 (pages 1-8) of McCarthy’s paper to answer the questions below.
Reading Notes
McCarthy uses mathematical notation that differs somewhat from Racket syntax. Here is a conversion from McCarthy’s notation to a syntax that would fit Racket.
- (p1 → e1, …, pn → en) would be written
(cond [p1 e1] ... [pn en]), where allpiandeiare expressions. - T is
#tand F is#f. - ∧ is boolean and (logical conjunction), ∨ is boolean or (logical disjunction), and ¬ is boolean not (logical negation). (The text defines these standard notations.)
- λ((x1,…,xn), e) would be
(lambda (x1 ... xn) e), wherex1throughxnare variable names andeis an expression.
Section 2 gets denser on pages 6-8. The most important material is earlier, but try to pick up what you can from the later parts as well.
Optional Reading
The remainder of McCarthy’s paper is an interesting read as well if you are curious. It describes more features of the Lisp language (still present in Racket) that we will consider briefly later. We will soon return to at least the section describing implementation.
Questions
Where asked for prose answers, please write no more than a few sentences.
-
McCarthy describes undefined results for some conditional expressions.
What does it mean for an expression’s result to be undefined? (McCarthy gives an indirect definition of this notion early in Section 2.)
-
Write two Racket expressions (using only the Racket features we have examined in this course) whose results are undefined, one using recursion and one without.
-
Write an evaluation rule for
cond(using the Racket-style syntax listed above and the style of evaluation rules we have defined in class) to describe the semantics of McCarthy’s conditional expression. -
Given the
condsyntax shown above and your evaluation rule, we can implementcondas syntactic sugar. Describe in English how to construct an equivalent Racket expression to replace acondexpression. Feel free to show concrete examples of replacement by an equivalent expression, but please describe the general transformation or “desugaring” you would perform given any validcondexpression.For inspiration, recall how we desugared the short-circuit and operation,
(and e1 e2), to the semantically equivalent expression(if e1 e2 #f))or how we desugared theorexpresion in class. If you need to produce an undefined result explicitly, call the zero argument functionvoid; it produces “no value.”