Questions about anything?
Note that next weekend is the long one between the first & second half of the semester, so we are not meeting next Monday.
Let's discuss this "data tagging" idea a bit and the "polymorphic dispatch" notion that I've asked you to read about this week.
I've put several examples in different languages in ~/code/types.
Here are a few places to read about these issues :
Related ideas : objects, message passing, operator overloading ...
It's a big topic with many variations and practical tradeoffs, handled differently in different languages. Your mission this week should be to understand some of the ups and downs of a few of the approaches as described in the text, hopefully made more concrete in the context of the lisp syntax we're using.
scheme :
(define a 2+3i) ;;; even though 2+3 is bad syntax. ;)
(display (* a a))
(display (+ a a))
python :
a = 2+3j
from cmath import phase
print(a.real, a.imag, abs(a), phase(a))
Complex numbers are one convenient way in python to do 2D geometry ... a "cute trick" way to do vector addition and subtraction in the xy plane.
Depending on our time, we may start to look at the material in chapter 3, which is where we're heading next.
Consider the following python code.
def make_accumulator(x):
balance = {'amount': x}
def add(y):
balance['amount'] += y
return balance['amount']
return add
a1 = make_accumulator(100)
a2 = make_accumulator(200)
print(a1(10)) # what is printed?
print(a1(10)) # what is printed now?
print(a2(1)) # how about this?
# Explain what's going on.
Does this work with just "balance = x" rather than a dict? Why or why not?
Can we do this in scheme ... or do we need something new?
Note that this is often called a "closure" ... though the textbook authors don't like that term in this context; they don't want to confuse the "programming" meaning with the "math" meaning.
This idea is going to lead us to the notion of an "environment", which is a new idea that we will need to model this sort of programming. The "substitution model" from the earlier chapter won't be enough.