[plt-scheme] graphics states and Scheme
On Jul 4, Dave Griffiths wrote:
> [...] The main thing is the scene description langauge, which
> currently involves an implicit state stack, and lots of small simple
> functions. [...]
I'm not sure that this is a proper suggestion, but I'll go anyway.
The thing is that when people use imperative code, they tend to forget
that using closures it's possible to push the imperative code into a
functional container. Here's a very simplified example -- assume that
this is your imperative library:
(define current-color 'black)
(define (set-color! c) (set! current-color c))
(define (draw-line len) (printf "drawing a ~a pixel ~a line\n" color len))
and you define
(define (draw-cube len color)
(set-color! color)
(draw-line len)
(draw-line len)
(draw-line len)
(draw-line len))
Instead of draw-cube, you can make it into a thunk that encapsulates
these operations:
(define (build-cube len color)
(lambda ()
(set-color! color)
(draw-line len)
...))
Once you have that, the rest becomes natural, for example:
(define (combine/offset obj1 obj2 ofs)
(lambda ()
(obj1)
(move-by ofs)
(obj2)))
(define (big-cube color)
(build-cube 100 color))
(define (rgb-cubes)
(let ([r (big-cube 'red)]
[g (big-cube 'green)]
[b (big-cube 'blue)])
(combine/offset (combine/offset r g 50)
b 50)))
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!