CS200: Computer Science, Spring 2002

Exam 1  Answers Out: 25 February 2002
Due: 27 February 2002, 11:00AM
Each question is worth 10 points. You get 30 points for showing the good judgement and wisdom to take this class, to make the total out of 100.
1. Describe the language defined by this recursive transition network using BackusNaur Form:
Question Average 8 or higher 1 7.8 4 2 9.6 9 3 8.7 6 4 7.8 4 5 8.2 7 6 7.6 3 7 7.4 5 Wisdom 30.0 9 Total 87.0
A digit is 0, 1, 2, ..., 9. The symbols inside circles (., +, and ) are terminals. Your BackusNaur Form grammar should not use the ^{*} shortcut notation. For full credit, your grammar should be the shortest possible grammar that describes the same language. Answer:constant ::= sign unsignednumber
sign ::= +
sign ::= 
sign ::=
unsignednumber ::= digits unsignednumber ::= digits . digits
digits ::= digit
digits ::= digits digitThere must be at least one digit. The most common mistake on this problem was to allow unsignednumbers with no digits, which is not possible with the RTN shown.
This RTN was based on the Pascal User Manual and Report by Kathleen Jensen and Niklaus Wirth (1975). It describes the Pascal language using both RTNs and BNF.
2. Ben Bitdiddle suggests implementing length, a procedure that takes a list as a parameter and evaluates to the number of elements in the list, by first replacing every element in the list with 1, and then summing the list. You may assume you have a procedure sumlist that takes a list of numbers and evaluates to the sum of all the numbers in the list.
For example, we can determine the length of the list (list 1 2 3 "alpha" "beta") by constructing the list (list 1 1 1 1 1) and evaluating (sumlist (list 1 1 1 1 1)) to get 5.
Ben started to define his length procedure below, but couldn't figure out what to put in the space where the box is. Finish the definition of length by providing a Scheme expression for the missing parameter to map:
(define (benslength lst) (sumlist (map _________________________________________ lst)))Answer:3. Louis Reasoner suggests that a general procedure to make a constant function would be useful for defining functions like benslength. That is, he would like a procedure that takes one parameter, and evaluates to a procedure that produces that value when applied to anything. For example,We want a procedure that takes one parameter and evaluates to 1:
(lambda (p) 1)
> ((makeconstantfunction "apple") "orange") "apple" > (((makeconstantfunction (makeconstantfunction 12)) "orange") 35) 12Define makeconstantfunction.
Answer:4. The benslength procedure (and the Scheme primitive length) counts only the elements in the toplevel list. For example, (benslength (list (list 1 2 3) 4)) ==> 2 since the inner list (list 1 2 3) only counts as one element. Alyssa suggests defining a lengthdeep procedure that evaluates to the total number of elements in a list and all its sublists. For example, (lengthdeep (list (list 1 2 3) 4)) ==> 4 and (lengthdeep (list (list (list 1 2) (list 3 4) (list 5 (list 6 7)))) ==> 7. Alyssa claims she can define lengthdeep using only these Scheme primitives: null?, list?, +, 0, 1, car and cdr. Define lengthdeep, a procedure with one list parameter that evaluates to the total number of elements in the list and all its sublists.(define (makeconstantfunction cst) (lambda (p) cst))Note that (makeconstantfunction 1) evaluates to:((lambda (cst) (lambda (p) cst)) 1) (Using Evaluation Name rule for makeconstantfunction)so we could answer question 2 now as (makeconstantfunction 1).
(lambda (p) 1) (Using compound application rule, binding 1 to cst)
Answer: This one was pretty tricky. The easiest is to define lengthdeep recursively. Note that we need to call lengthdeep for both the car and cdr of the list, since the car might be a list. Here's how:5. Define a procedure reverse that takes a list parameter and evaluates to a list containing the elements of the original list in reverse order. You may find it useful to define extra procedures also. You may not use the Scheme primitive reverse procedure. For this question, do not use insertlg. Example:(define (lengthdeep lst) (if (null? lst) 0 (+ (if (list? (car lst)) (lengthdeep (car lst)) 1) (lengthdeep (cdr lst)))))A different approach was to first flatten the list, and then use sumlist:(define (flattenlist slst) (if (null? slst) slst (if (list? (car slst)) (append (flattenlist (car slst)) (flattenlist (cdr slst))) (cons (car slst) (flattenlist (cdr slst)))))) (define (lengthdeep lst) (benslength (flattenlist lst)))> (reverse (intsto 5)) (5 4 3 2 1)Answer: We can think of reversing a list recursively: we add the first element of the list to the end of reversing the rest of the list. For example, to reverse (1 2 3 4 5) we put 1 at the end of the result from reversing the list (2 3 4 5).6. Define reverse (with the same meaning as the previous question) using insertlg.A common (but not quite correct) answer was:
(define (mreverse lst) (if (null? lst) lst (cons (mreverse (cdr lst)) (car lst))))This puts the elements in the reverse order, but does not produce a proper list. For example (reverse (intsto 5)) would evaluate to (((((() . 5) . 4) . 3) . 2) . 1).One solution would be to use append instead of cons:
(define (reverse lst) (if (null? lst) lst (append (reverse (cdr lst)) (list (car lst)))))Another approach would be to turn the result of the first implementation into a proper list. We do this by defining a procedure listify, similar to flattenlist used above:(define (listify slst) (if (null? slst) slst (if (pair? slst) (append (listify (car slst)) (listify (cdr slst))) (list slst)))) (define (reverse lst) (flattencons (mreverse lst)))
Answer:7. Orders of Growth(define (reverse lst) (insertlg (lambda (x rest) (append rest (list x))) lst null))Express your answers using Θ and n for the length of the list. Assume cons, car, cdr, and null? take constant time (that is, the time they take does not depend on the length of the list).
 How much work is your reverse procedure from Question 5.
 How much work is your reverse procedure from Question 6.
Answer: For the definition using append:(define (reverse lst) (if (null? lst) lst (append (reverse (cdr lst)) (list (car lst)))))We evaluate append once for each sublist, that is n times total. Each append is Θ(n), assuming the definition from SICP:(define (append list1 list2) (if (null? list1) list2 (cons (car list1) (append (cdr list1) list2))))This does one cons for each element in the first lists. So, reverse as defined about is Θ(n^{2}).The definition using insertlg is also Θ(n^{2}). It evaluates append exactly the same way as the previous definition.
There are ways to reverse a list in Θ(n), but its surprisingly tricky to do this in Scheme. If you can do this, you get 10 points added to your exam score. You might want to start by considering the listify version above (which is not Θ(n), but could be turned into something that is). You will know your answer is correct if the number of cons evaluations for reversing (intsto 10) is twice the number for reversing (intsto 5).
University of Virginia Department of Computer Science CS 200: Computer Science 
David Evans evans@virginia.edu Using these Materials 