Computer Science 61A - Spring 1995 - Clancy - Final

Computer Science 61A - Spring 1995 - Clancy - Final -...

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

View Full Document Right Arrow Icon
CS61A: Spring 1995 Final 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. file:///C|/Documents%20and%20Settings/Jason%20Rafte. ..20Spring%201995%20-%20Clancy%20-%20Final%20Exam.htm (1 of 6)1/27/2007 6:28:49 PM
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS61A: Spring 1995 Final 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 (list-number position-within-list) so for the stream shown above you would produce a stream with elements {(0 1) (0 4) (1 0) (1 1) (1 2) (1 4) (1 5) (2 3) (4 0) . ..} but not necessarily in the order shown. The elements may appear in any order in your result stream,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Computer Science 61A - Spring 1995 - Clancy - Final -...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online