[plt-scheme] Re: from hell to paradise

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Feb 17 22:19:50 EST 2009

On Feb 17, Matthias Felleisen wrote:
> 
> Let's build:
> 
>   -- a software simulation test bed for robots
>   -- with an interface that's like the stupid robots BW will program
>   -- and let's demo in your course how 'program design' can do much
> much better than his programming

A possibly stupid question: did anyone look into using thunks to turn
imperative style programs into functional ones?  I mean, not doing so
with the intention of designing a course on something like robotics
better, but instead done with the direct intention of doing a second
course about software design (FP-style) but not throwing away
completely stuff that students were exposed to in the first course.

Basically something like:

  * They learned how to write programs that "do" things, like move a
    robot N mm forward and turn left N degrees, in the first course.

  * The second course begins with explaining thunks and how they can
    be composed.

  * At this point it should be possible to tanslate knowledge from the
    old course:

      (define (circle left?)
        (let ([n 0])
          (while (< n 360)
            (move-forward 1)
            (turn-left (if left? 1 -1))
            (set! n (add1 n)))))
      (define (8-shape)
        (circle #t)
        (circle #f))

    into its functional form (using some simple sugars):

      (define (circle left?)
        (local [(define fwd  (thunk (move-forward 1)))
                (define turn (thunk (turn-left (if left? 1 -1))))
                (define step (combine fwd turn))]
          (build-combination 360 (lambda (i) step))))
      (define 8-shape (combine (circle #t) (circle #f)))

I can help but think that this must be a good idea because (a) it
builds on what they learned in the for-the-popular-masses course
instead of throwing it all out, and (b) it shows how to use first
class functions in a way that will be useful for students in the
future too -- given that most of them will go on living in an
imperative world, but also given that in practically all modern
languages you have first class functions.  They'll have the basic
tools for translating such an imperative world to a functional one.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.