CS61A_sp95_f

# CS61A_sp95_f - CS61A Spring 1995 Final CS61A Spring 1995...

This preview shows pages 1–3. Sign up to view the full content.

CS61A, Spring 1995 Final Question 1 (5 points): (a) Write a function prefix-to-infix that takes a Scheme arithmetic expression as its argument and returns a list containing the equivalent expression in the ordinary arithmetic notation with operators between the operands, like this: > (prefix-to-infix '(+ (* 2 3) (- 7 4))) ((2 * 3) + (7 - 4)) > (prefix-to-infix '(* (remainder 9 2) 5) ((9 remainder 2) * 5) You may assume that every function in the argument expression has exactly two arguments. (b) The procedure you wrote in part (a) carries out a tree reordering; in each sublist, the three elements are rearranged from the original order (0 1 2) to the new order (1 0 2) . That is, what used to be element number 1 now comes first; what used to be element number 0 now comes second, and what used to be number 2 remains third. We can represent this ordering by the list (1 0 2) . We'd like to generalize this by writing a procedure that takes an ordering as an additional argument, so that we could say (define (prefix-to-infix tree) (tree-reorder '(1 0 2) tree)) Here's an example of a tree-reorder that isn't a prefix-to-infix : > (tree-reorder '(2 1 2 1) '((a b c) (d e f) (g h i))) ((i h i h) (f e f e) (i h i h) (f e f e)) What follows is a partial implementation; your job is to fill in the blank. (define (tree-reorder ordering tree) (if (atom? tree) tree (map _______________________________________________________ ordering))) Assume that no number in the ordering is bigger than the length of any sublist; no error checking is needed. Question 2 (5 points): You are given a possibly infinite stream of lists of numbers. In the following example, the notation {...} represents a stream, while (...) represents a list: {(0 1 0 0 3) (1 2 3 0 4 2) (0 0 0 5 0) () (3) . ..} Write a procedure positions that, given such a stream as its argument, returns a stream of two-element lists showing the positions of the nonzero numbers within the argument. Each two-element list has the form CS61A: Spring 1995 Final 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
(list-number position-within-list) so for the stream shown above you would produce a stream with elements
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at Berkeley.

### Page1 / 5

CS61A_sp95_f - CS61A Spring 1995 Final CS61A Spring 1995...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online