[plt-scheme] Imperative programming : missing the flow

From: Anton van Straaten (anton at appsolutions.com)
Date: Sat May 12 21:58:35 EDT 2007

John Clements wrote:
> I'm teaching Java to first-year students, and we're about to walk off  a 
> cliff and start programming imperatively.  I've done this  transition 
> several times now, and it occurs to me that what's most  irritating to 
> me about the imperative style is the lack of "flow".
> 
> What do I mean by flow?  Simply that each expression produces a  result 
> that is used by another expression.  In imperative  programming, by 
> contrast, I feel that I have a bunch of statements  lying on the floor, 
> and that their order is both important and  difficult to explain clearly.

Perhaps this is too obvious to mention in the present company, but since 
you didn't mention it, the world-passing (monadic) view of imperative 
programming might provide a useful perspective.  It provides an easily 
understood theory (at a high level) of imperative programming, which 
lets you compare imperative programs to functional programs using a 
common model.  (Admittedly, the model is skewed in favor of the 
functional approach, but I'm guessing you won't object to that.)

The resulting explanations end up not being all that different from 
those that have already been given, but introducing "the world" as a 
first-class feature of the explanatory model helps to discuss and reason 
about the issues.

For example, the world-passing perspective explains why the imperative 
puzzle pieces (statements) that Joe described are perfect squares: the 
only functional/algebraic dependency between statements is "the world", 
so there's no way to fit statements together based on what kind of value 
they consume and produce -- they all consume and produce a version of 
the world.

Another point is that every statement in an imperative program can 
change the value of any currently visible variable in the world.  In 
contrast, a functional program can only "change" the value of variables 
at function application time.  So every single imperative statement is 
like a function in its own right, which complicates reasoning.

If the students can handle a functional model of an imperative program 
looking something like:

   ((lambda (world) stmt-n)
    ((lambda (world) ...)
     ((lambda (world) stmt-1)
      '())))

...then there are quite a few concrete points and comparisons that could 
be made with it.  It's also good preparation for teaching monads later... :)

Anton



Posted on the users mailing list.