![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
Hey, please don't forget the methodology used every hw, lect: (from lect02b):
Design recipe: 1. data def'n 2. examples of data 3. write function header, signature, purpose 4. case anlaysis 5. inventory 6. natural recursion 7. fill inThis is Figure 36 in How to Design Programs. (See the related Figs. 35 and 34, too.)
Go over lect08-Ancestor-Trees-java/.
Write
Btw, compare this to the traditional approach to trees in CS2:
in our approach
we don't actually mention
class Node { String name; Node left; // well, a real Node *or* null Node right; // well, a real Node *or* null } |
The problem stems from the fact that the recursive data definition
has three types
(
Note that
Haskell has an interesting approach: They use the composite pattern to represent the union of null-or-something. In java-ish terms:
// A non-helpful Java implementation of Haskell's Maybe/Just/Nothing types: abstract class Maybe<T> {} <T> class Nothing extends Maybe<T> {} // The "null" case. class Just<T> extends Maybe<T> { T val; Maybe(T _val) { this.val = val; } } |
Btw:
Objects.html
Note that this class is created so that we have null-safe variants of common methods;
they are therefore necessarily static methods.
How many ways are there, to call a function in Java?
Java: obj.meth(...) [also with implicit 'this'] Class.meth(...) a + b (+ a b) !a (not a) new int[23] (make-vector 23 0) or: (build-vector 23 (lambda (i) (* 3 i))) arr[i] (vector-ref arr i) Btw, compare to List.get(i) arr[i] = ... (vector-set! arr i ...) Btw, compare to List.set(i,...) new Widget(...) (make-widget ...) super(...) this(...) obj.f (widget-f obj) obj.f = ... (set-widget-f! obj ...) (String literals call string constructor?)
Although "assigning to a field" and "assigning to a variable" look virtually the same
in Java, they are really very different.
In racket, they have different names:
obj.f = ... (set-widget-f! obj ...) int a = 17; (define a 17) a = 99; (set! a 99)There is a good reason to treat them differently:
int a = 17; // some code that doesn't mention `a` System.out.print( a ); // MUST be 17. Widget obj = ... obj.f = 17; // some code that doesn't mention `obj.f`, or even `obj` System.out.print( obj.f ); // might have been modified!Keep in mind that Java is pass-by-value, but that one type of value is "object-reference". So in the above, the variable
lect09c: Show grammar for N0; give examples of expressions *and* parse trees; give internal-representation data def'n, and examples Discuss what read,eval,print should do, and go over the recursive-descent parsing.
lect10a: Give an example for testing `parse` (expected result: a tree); talk about `substitute`. This led to discussions re how identifiers will be represented.
home—lectures—exams—hws—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Nov.02 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |