Due to the exam, this homework assignment is a bit lighter than a regular assignment. However, there are still some challenging problems here so please get started early!
In this assignment you will be implementing a number of interesting rex functions to build up your rex programming muscles! You may define whatever additional helper functions you like in order to help implement these functions, unless explicitly stated otherwise.You may also use any rex built-in function that we've discussed in class. As it turns out, the following built-in functions suffice for this assignment: append, map, turns the larger of its two numerical inputs), first (takes as input a list and returns the first element in that list), and second (takes as input a list and returns the second element in that list).
Please place all of your functions in a single file called hw6.rex. Please use the standard commenting convention of placing your name, file name, and date at the top of the file. For each of the problems below, include a comment (Java comment syntax works in rex) indicating which problem this is (such as "this is the code for the blah blah blah problem"). Please make sure that your solutions appear in the order that we have given the problems. Use white space (spaces, new lines, tabbing) to make your code readable. We are not enforcing any particular formating convention for your code, but do avoid writing long functions all on one line. That's ugly.
Finally, please BE SURE TO NAME YOUR FUNCTIONS EXACTLY THE SAME AS WE'VE SPECIFIED BELOW. The grading procedure for this assignment is partially automated and functions whose spellings differ at all from those below will not be seen by the automatic testing programs.
rex > supereverse([ [1, 2, 3], [4, 5] ]); [ [3, 2, 1], [5, 4] ]
rex > erdos(42);
21
rex > erdos(109);
328
erdos(erdos(erdos(3))) = 16.Continuing in this way,
erdos(erdos(erdos(erdos(erdos(erdos(erdos(3))))))) = 1with erdos iterated 7 times. Thus, collapse([3]) == [7]. Further examples of collapse in action include
rex > collapse([3, 10, 5, 16]); [7, 6, 5, 4] rex > collapse([129, 55, 85]); [121, 112, 9]Will every positive integer collapse to 1? No one knows, though many mathematicians have worked on it. The great mathematician Paul Erdos (the most prolific mathematician of all time) actually felt that "mathematics is not yet ready for such problems." This problem is known as the Collatz problem or "3x+1" problem. If you're in the hallways of Pomona's math department, check out the poster there on this very famous problem and what's currently known about it! You might also wish to Check out this site for more information on the amazing Paul Erdos.
// Here I'm using the erdos function from the example above. // since the first argument is 1, the desired result is erdos(3), // and erdos(3) equals 10. rex > iterate(1, erdos, 3); 10 // Now I'm iterating twice and computing erdos(erdos(3)): rex > iterate(2, erdos, 3); 5 // And this is finding erdos(erdos(erdos(3))) rex > iterate(3, erdos, 3); 16 // Iterating the anonymous function below two times is equivalent to adding 2. rex > iterate(2, (x)=>x+1, 9); 11
// Here I'm using the erdos function from the example above. // since the first argument is 1, the desired result is erdos(3), // and erdos(3) equals 10. rex > iterate(1, erdos)(3); 10 // Now I'm iterating twice and computing erdos(erdos(3)): rex > iterate(2, erdos)(3); 5 // And this is finding erdos(erdos(erdos(3))) rex > e3 = iterate(3, erdos); 1 rex > e3(3); 16 // Iterating the anonymous function below two times is equivalent to adding 2. rex > iterate(2, (x)=>x+1)(9); 11
// To demonstrate, I'll first define two functions called foo and goo.
rex > foo(X) => 2*X + 1;
foo
rex > goo(X) => 3*X - 2;
goo
// Now, envelope(foo, goo) returns a function and here we let moo
// represent that function.
rex > moo = envelope(foo, goo);
1
// Now, let's evaluate the function moo with input of 5.
rex > moo(5);
13
Write a predicate reachable(a,b,G) that answers the question of whether or not you can get from city "a" to city "b" in the directed graph G. Specifically, the first two inputs to reachable are nodes in the graph G, and the graph itself is the third input. reachable returns 0 if there is no path from a to b, and it returns 1 if there is a path (possibly through other nodes) from a to b.
NOTE Your predicate will only be given ACYCLIC graphs as input. Thus, you don't have to worry about "running around in circles" searching for whether there is a path from a to b, because there will be no cycles in the graph. Also, remember the graphs are directed. For example,
rex > reachable("A", "F", [ ["A","D"], ["B","D"], ["C","D"], ["A","E"],
["D","E"], ["E","F"] ]);
1
rex > reachable("C", "A", [ ["A","B"], ["B","C"] ]);
0
rex > reachable("D", "D", [ ["A","B"], ["B","C"] ]);
1
As the last example illustrates, your function should acknowledge
that any node can be reached from itself. What's more the node
does not even need to be in the graph! (This is because the
graph does not have to be connected -- an unlisted node
is one with neither outgoing nor incoming edges.)
rex > mytree = [10, [3, [], []], [42, [20, [], []], [60, [], []] ]]; 1 rex> minimum(mytree); 3
rex > mytree = [10, [3, [], []], [42, [20, [], []], [60, [], []] ] ]; 1 rex > delete(10, mytree); [20, [3, [], []], [42, [], [60, [], []]]]
mytree = ["Ada",
["Betty",
["Cary"],
["Corwin"],
["Csilla"],
["Cyrus"]
],
["Bob"],
["Boris",
["Duvan",
["Esmerelda"],
["Egbert"]
]
]
];
Notice that unlike our representation of binary trees, in this
problem
we assume a tree representation in which a
leaf node is a list with just one element (as opposed to one element
with two null children). Your job is to write a function called
leaves(L) which takes as input an arbitrary multi-way tree
and returns a list of all leaves in the tree (nodes with no children).
For example, for the tree defined above, sample input and output would
look like this:
rex > leaves(mytree); [Cary, Corwin, Csilla, Cyrus, Bob, Esmerelda, Egbert]