Algorithms and OOD (CSC 207 2014F) : EBoards

# CSC 207: Extra Session 3 (Thursday, 18 Sept. 2014)

How does `check-equal?` work in Scheme?

• Step one: Check that types match. No? Then the values aren't equal.
• Step two: Compare using a type-specific approach
• Booleans: Easy
• Symbols: Easy
• Strings: Compare character by character.
• Numbers: Easy (?)
• Fixed precision: Bits!
• Arbitrary precision: Length + Digits (or equivalent)
• Vectors or lists:
• Compare lengths
• Compare elements recursively
• Procedures:
• Same memory location
• But, um, it appears to be a bit more complex than that to deal with looping structures.

Is `String.equal` in Java comparing memory location or character by character?

• Run experiments!

Can you as programmer find out the location of an object?

• ((Object) obj).toString() may give the "identifier" associated with an object. That may or may not be the memory location.
• Even if you know the memory location, there is no Java command that takes a memory location as a parameter. So it's useless.

For the new calculator, what are the registers for?

• Registers store fractions.
• Our calculator is the implementation of a baby programming language.

``````> 1/2 + 2/3
5/6
> r1 = 1/2 + 2/3
5/6
> r1
5/6
> r1 * r1
25/36
``````

When you say "rewrite this recursive algorithm iteratively", what do you mean?

• Use the same general idea as that algorithm, but expressed iteratively.
• Your result should still take time proportional to the log_2 of the exponent.

Have we done user input yet?

• Um. kinda. But you've read about it. And maybe a few small problems.

How do we make a prompt?

``````    PrintWriter pen = new PrintWriter(System.out, true);

...
pen.print("> ");
pen.flush();
result = eyes.readLine(); // I think
``````

What is the relationship between `StringBuffer` and `StringBuilder`?

• One works for multithreaded computation, the other doesn't.

What do you mean by "Make a template class"?

• Include a constructor so that you can have multiple calculators.
• The registers will be fields of the calculator.

Can you explain the difference between functional, imperative/procedural, and object-oriented programming?

• Note: Almost everything we do mixes paradigms.
• Each has an emphasis/model.
• We can do "pure" functional or oop, but we rarely do.
• Functional:

• Functions are first-class values.

• I can write a function that creates a new function that did not exist previously.

``````> (define make-adder
(lambda (x)
(lambda (y) (+ x y))))
7
``````
• C has pointers to functions, but not the ability to make new functions
• I can write procedures that take functions as input. (You can do this in C, too.)
• I can write anonymous procedures.
• Traditionally: Operations don't have side effects, so order of evaluation doesn't matter too much.
• Often: `eval` - Code and data are interchangable

``````    > (define exp (list '+ 1 2))
> exp
'(+ 1 2)
> (eval exp)
3
``````
• OOP
• Programs are collections of interacting objects that send messages to each other.
• Three characteristics
• Encapsulation
• Inheritance
• Polymorphism
• Imperative/Procedural
• Explicit sequencing of operations
• Basic operations are calculation, input, output, and changing memory

Can you give a concrete example that suggests the difference?

• Two points in n-space. Find their distance.
• Imperative programmer int temp = 0; for (i = 0; i < n; i++) temp += square(p1[i] - p2[i]); return sqrt(temp);
• Mathematician "square root of the sum of squares of differences"
• Functional (sqrt (reduce + (map square (map - p1 p2))))
• OOP p1.subtract(p2).each(square).sum().sqrt()

Can you tell us about Exceptions?

• no. it's a topic for tomorrow.

When building an array on the fly, should I use a linked list or an array list or a vector or ...?

• Think about the way in which you are using it.

Can you help me with the homework that I got an extension on and everyone else is done with?

• if there's time left at the end of class. but there's not. sorry.