This assignment is 50 points of your overall grade. Each problem is worth 8 points.
You are asked to construct several function definitions in rex and test them. You should submit your functions in a single file clearly commented. You don't have to write a book on each one, but at least describe the function and, if more than one or two lines, your approach. In addition, be sure to have your name at the top of the file. The example file factorial.rex in the directory /cs/cs60/assignments/a1 demonstrate appropriate commenting styles.
This assignment touches on parts of the text through Chapter 3, though the topics actually needed will be considered in class. You should, however, begin reading the book, as there are many more examples and thorough explanations of rex's features and the information structures used here. You may want to try writing some simpler functions on your own as well to get the hang of rex.
rex > add42(-7); 35 rex > add42(190); 232If the user provides an input which is not a number, that's not your problem!
rex > test(add42(-7),35); ok: add42(-7) ==> 35 rex > test(add42(190),232); ok: add42(190) ==> 232In general, it is usually much easier to use emacs (or your favorite text editor) to create and modify files of rex statements and then "include" them as described above. There are files in the directory
rex > *i /cs/cs60/assignments/a1/p0test.rex 1 rex > ok: add42(-7) ==> 35 2 rex > ok: add42(0) ==> 42 3 rex > ok: add42(101) ==> 143 4 rex > ok: add42(190) ==> 232 5 rex > ok: add42(12345678900) ==> 12345678942You may want to copy the test files to your directory or file to save on typing the path name.
The graders will test your functions on these and other examples. When the problem states that a function's argument has a given form, you may assume that the test cases will not take any other forms; that is, you do not need to put in error-checking for erroneous argument types. Each problem will receive credit according to the following guidelines:
rex > add42List([1, -7, -42, 8]);
[43, 35, 0, 50]
(Use map.)
rex > sum([5, 8, 9]);
22
rex > sum(range(1,10));
55
(Use reduce.)
rex > average([5, 8, 9]);
7.33333
rex > average(range(1,10));
5.5
You will want to use the float function, along with the
length of the list, in order to obtain floating point
averages. Note that
rex > 22/3;
7
rex > 22/float(3);
7.33333
rex > power(3,4);
81
rex > power(10,3);
1000

rex > superpower(2,3);
16
rex > superpower(3,3);
7625597484987
(You might want to check out superpower(2,5), but
not superpower(2,6).)
rex > superreverse([ [1,2,3], [4,5,6], [7,8,9] ])
[ [3,2,1], [6,5,4], [9,8,7] ]
rex > superreverse([['o','l',['I']],['e','v'],[['e','x'],'r']]);
[[[I], l, o], [v, e], [r, [e, x]]
rex > duperreverse([1, [2, 3], [4, [5, 6, [7, 8], 9] ] ]);
[[[9, [8, 7], 6, 5], 4], [3, 2], 1]
You will want to use the predicate atomic to implement this
function.
letterScores =
[
['a',1,9],
['b',3,2],
['c',3,2],
['d',2,4],
['e',1,12],
['f',4,2],
['g',2,3],
['h',4,2],
['i',1,9],
['j',8,1],
['k',5,1],
['l',1,4],
['m',3,2],
['n',1,6],
['o',1,8],
['p',3,2],
['q',10,1],
['r',1,6],
['s',1,4],
['t',1,6],
['u',1,4],
['v',4,2],
['w',4,2],
['x',8,1],
['y',4,2],
['z',10,1]
]
(This list is formatted for direct copying to a rex-readable file.) Write
a function scrabbleScore, which takes a word (as a string) and
the above letterScores list as inputs and outputs the score for
that word. Thus,
rex > scrabbleScore("twelve",letterScores);
12
rex > scrabbleScore("quiz",letterScores);
22
The built-in function explode will be useful for this problem: it
takes a string as input and outputs the same string and a list of
characters, e.g.,
rex > explode("rex"); // in case you're thinking regicide by now...
[ r, e, x ]
where those three characters are literal characters and not
variables.
sys(in,"/cs/cs60/assignments/a1/threeLets.rex");
either typed at the prompt or placed in your hw1.rex file, the
variable ospd3 will be bound to a list of all legal three-letter
words.
rex > bestThree("aaeiijx",ospd3);
[10, axe]
rex > bestThree("abcdabc",ospd3);
[7, cab]
rex > bestThree("eiilrrx",ospd3);
[10, rex]
For totally optional bonus credit, write an improved scrabble-scoring
function, hardScrabble, which takes into account the fact that
there are a limited number of tiles of each letter available in the game and
that there are two blank tiles, worth no points, which act as wildcards.
For example, one implementation might act as follows:
rex > scrabbleScore("fuzzy");
29
rex > scrabbleScore("pizzazz");
45
rex > hardScrabble("fuzzy");
19
rex > hardScrabble("pizzazz");
Impossible word!