Posts tagged Teaching
Okay, it’s week four of data structures in Python. In the past few days, I’ve read a lot of terrible code. Here’s a beautiful, horrible, example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 
# An IntList is one of
#  None, or
#  Pair(int, IntList)
class Pair:
def __init__(self, first, rest):
self.first = first
self.rest = rest
# standard definitions of __eq__ and __repr__ ...
# A Position is one of
#  an int, representing a list index, or
#  None
# IntList int > Position
# find the position of the sought element in the list, return None if not found.
def search(l, sought):
if l == None:
return None
rest_result = search(l.rest, sought)
if (l.first == sought or rest_result) != None:
if l.first == sought:
return 0
else:
return 1 + rest_result

This code works correctly. It searches a list to find the position of a given element. Notice anything interesting about it?
Take a look at the parentheses in the if
line. How do you feel about this code now?
(Spoilers after the jump. Figure out why it works before clicking, and how to avoid this problem.)
It’s much closer to ‘go’ time now with Python, and I must say, getting to know Python better is not making me like it better. I know it’s widely used, but it really has many nasty bits, especially when I look toward using it for teaching. Here’s my old list:
 Testing framework involves hideous boilerplate.
 Testing framework has standard problems with floatingpoint numbers.
 Scoping was clearly designed by someone who’d never taken (or failed to pay attention in) a programming languages course.
 The vile ‘return’ appears everywhere.
But wait, now I have many more, and I’m a bit more shouty:
 Oh dear lord, I’m going to have to force my students to implement their own equality method in order to get testcaseintensional checking. Awful. Discovering this was the moment when I switched from actually writing Python to writing Racket code that generates Python. Bleah.
 Python’s timing mechanism involves a hideously unhygienic “pass me a string representing a program” mechanism. Totally dreadful. Worse than C macros.
 Finally, I just finished reading Guido Van Rossum’s piece on tailcalling, and I find his arguments not just unconvincing, not just wrong, but sort of deliberately insulting. His best point is his first: TRE (or TCO or just proper tailcalling) can reduce the utility of stack traces. However, the solution of translating this code to loops destroys the stack traces too! You can argue that you lose stack frames in those instances in which you make tail calls that are not representable as loops, and in that case I guess I’d point you to our work with continuation marks. His next point can be paraphrased as “If we give them nice things, they might come to depend on them.” Well, yes. His third point suggests to me that he’s tired of losing arguments with Scheme programmers. Fourth, and maybe this is the most persuasive, he points out that Python is a poorly designed language and that it’s not easy for a compiler to reliably determine whether a call is in tail position. Actually, it looks like he’s wrong even here; I read it more carefully, and he’s getting hung up on some extremely simple scoping issues. I’m really not impressed by GvR as a language designer.
Earlier this year, I was talking to Kurt Mammen, who’s teaching 357, and he mentioned that he’d surveyed his students to see how much time they were putting into the course. I think that’s an excellent idea, so I did it too.
Specifically, I conducted a quick endofcourse survey in CPE 430, asking students to estimate the number of weekly hours they spent on the class, outside of lab and lecture.
Here are some pictures of the results. For students that specified a range, I simply took the mean of the endpoints of the range as their response.
Then, for those who will complain that a simple histogram is easier to read, a simple histogram of roundedtothenearesthour responses:
Finally, in an attempt to squish the results into something more accurately describable as a parameterizable normal curve, I plotted the density of the natural log of the responses. Here it is:
Sure enough, it looks much more normal, with no fat tail to the right. This may just be data hacking, of course. For what it’s worth, the mean of this curve is 2.13, with a standard deviation of 0.49.
(All graphs generated with Racket.)
I’m just getting started, but already Python is looking like a terrible teaching language, relative to Racket.
 Testing framework involves hideous boilerplate.
 Testing framework has standard problems with floatingpoint numbers.
 Scoping was clearly designed by someone who’d never taken (or failed to pay attention in) a programming languages course.
 The vile ‘return’ appears everywhere.
So here I am grading another exam. This exam question asks students to imagine what would happen to an interpreter if environments were treated like stores. Then, it asks them to construct a program that would illustrate the difference.
They fail, completely.
(Okay, not completely.)
By and large, it’s pretty easy to characterize the basic failing: these students are unwilling to break the rules.
One of my children is in third grade. As part of a “backtoschool” night this year, I sat in a very small chair while a teacher explained to me the “Math Practices” identified as part of the new Common Core standards for math teaching.
Perhaps the small chair simply made me more receptive, taking me back to third grade myself, but as she ran down the list, I found myself thinking: “gosh, these are exactly the same skills that I want to impart to beginning programmers!”
Here’s the list of Math Practices, a.k.a. “Standards for Mathematical Practice”:
 Make sense of problems and persevere in solving them.
 Reason abstractly and quantitatively.
 Construct viable arguments and critique the reasoning of others.
 Model with Mathematics.
 Use appropriate tools strategically.
 Attend to precision.
 Look for and make use of structure.
 Look for and express regularity in repeated reasoning.
Holy Moley! Those are incredibly relevant in teaching programming. Furthermore, they sound like they were written by someone intimately familiar with the How To Design Programs or Bootstrap curricula. Indeed, in the remainder of my analysis, I’ll be referring specifically to the steps 1–4 of the design recipe proposed by HtDP (as, e.g., “step 2 of DR”).
Let’s take those apart, one by one:
Being on sabbatical has given me a bit of experience with other systems and languages. Also, my kids are now old enough to “mess around” with programming. Learning from both of these, I’d like to hazard a bit of HtDP heresy: students should learn for i = 1 to 10
before they learn
To many of you, this may seem obvious. I’m not writing to you. Or maybe you folks can just read along and nod sagely.
HtDP takes this small and very lovely thing—recursive traversals over inductively defined data—and shows how it covers a huge piece of real estate. Really, if students could just understand how to write this class of programs effectively, they would have a vastly easier time with much of the rest of their programming careers, to say nothing of the remainder of their undergraduate tenure. Throw a few twists in there—a bit of mutation for efficiency, some memoization, some dynamic programming—and you’re pretty much done with the programming part of your first four years.
The sad thing is that many, many students make it through an entire fouryear curriculum without ever really figuring out how to write a simple recursive traversal of an inductively defined data structure. This makes professors sad.
Among the Very Simple applications of this nice idea is that of “indexes.” That is, the natural numbers can be regarded as an inductively defined set, where a natural number is either 0 or the successor of a natural number. This allows you to regard any kind of indexing loop as simply a special case of … a recursive traversal of an inductively defined data structure.
So here’s the problem: in September, you face a bunch of brighteyed, enthusiastic, deeply forgiving firstyear college students. And you give them the recursive traversal of the inductively defined data structure. A very small number of them get it, and they’re off to the races. The rest of them struggle, and struggle, and finally get their teammates to help them write the code, and really wish they’d taken some other class.
NB: the rest of this makes less sense… even to me. Not finished.
However, another big part of the problem is … well, monads are like burritos.
Let me take a step back.
The notion of repeated action is a visceral and easilyunderstood one. Here’s what I mean. “A human can multiply a pair of 32bit integers in about a minute. A computer can multiply 32bit integers at a rate of several billion per second, or about a hundred billion times as fast as a person.” That’s an easilyunderstood claim: we understand what it means to the same thing a whole bunch of times really fast.
So, when I write
for i=[1..100] multiply_two_numbers();
It’s pretty easy to understand that I’m doing something one hundred times.