[plt-scheme] graphics states and Scheme
Maybe of interest:
Functional Geometry. http://www.frank-buss.de/lisp/functional.html
On 7/4/07, Eli Barzilay <eli at barzilay.org> wrote:
> 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!
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
--
Cheers,
Stephen
--
Stephen De Gabrielle
s.degabrielle at ucl.ac.uk
Telephone +44 (0)20 7679 5242 (x45242)
Mobile 079 851 890 45
http://www.uclic.ucl.ac.uk/annb/MaSI.html
University College London Interaction Centre
Remax House - 31/32 Alfred Place
London - WC1E 7DP