;A variation of drawSystem.scm where all uses of ; (set! ) have been removed. Functions ; which in drawSystem.scm update the state of the ; turtle, in drawsystem-no_set.scm take the turtle ; state as an argument and return a list that ; consists of the updated turtle state. Both ; drawSystem.scm and drawsystem-no_set.scm have ; exactly the same number of recursive calls. (load "writeln.scm") (define drawSystem (lambda (cmdList) ;(define x 0) ;(define y 0) ;(define angle 0) ;; Make an 800 x 500 frame (define frame (instantiate frame% ("L-System") (width 800) (height 500)) ) (define draw-SetUp (lambda (dc) (send dc set-smoothing 'smoothed) (send dc set-pen red-pen) ;Start state of the turtle is now set as ; an argument to the first recersive call ; to draw-cmdList ;(set! x 400) ;(set! y 250) ;(set! angle 0) ) ) (define processf (lambda (dc turtle) ;(define dx 0) ;(define dy 0) (define x (car turtle)) (define y (car (cdr turtle))) (define angle (car (cdr (cdr turtle)))) ;(cond ; ((eq? angle 0) (set! dy -20)) ; ((eq? angle 90) (set! dx 20)) ; ((eq? angle 180) (set! dy 20)) ; ((eq? angle 270) (set! dx -20)) ;) ;(let ((x1 (+ x dx)) (y1 (+ y dy))) ; (send dc draw-line x y x1 y1) ; (set! x x1) ; (set! y y1) ;) ;processf returns the updated turtle state. ;The quasiquotation with the common override ;is used to return a list where the first ;element is not evaluated as a function, and ;but where the value of each element is ;returned. (cond ((eq? angle 0) ;(set! dy -20)) (send dc draw-line x y x (- y 20)) `(,x ,(- y 20) ,angle) ;return turtle ) ((eq? angle 180) ;(set! dy +20)) (send dc draw-line x y x (+ y 20)) `(,x ,(+ y 20) ,angle) ;return turtle ) ((eq? angle 90) ;(set! dx +20)) (send dc draw-line x y (+ x 20) y) `(,(+ x 20) ,y ,angle) ;return turtle ) ((eq? angle 270) ;(set! dx -20)) (send dc draw-line x y (- x 20) y) `(,(- x 20) ,y ,angle) ;return turtle ) ) ) ) (define setAngle (lambda (delta turtle) (let ((angle (car (cdr (cdr turtle))))) ;(set! angle (modulo (+ angle delta) 360)) (writeln "(car turtle)" (car turtle)) `(,(car turtle) ,(car (cdr turtle)) ,(modulo (+ angle delta) 360) ) ) ) ) (define draw-cmdList (lambda (dc mylist turtle) (writeln "draw-cmdList" turtle) (cond ((null? mylist) 0) (else (let ((c (car mylist))) (cond ;((eq? c '+) (setAngle 90)) ;((eq? c '-) (setAngle -90)) ;((eq? c 'f) (processf dc)) ((eq? c '+) (draw-cmdList dc (cdr mylist) (setAngle 90 turtle))) ((eq? c '-) (draw-cmdList dc (cdr mylist) (setAngle -90 turtle))) ((eq? c 'f) (draw-cmdList dc (cdr mylist) (processf dc turtle))) ) ;(draw-cmdList dc (cdr mylist)) ) ) ) ) ) ;; Make the drawing area with a paint callback (define canvas (instantiate canvas% (frame) (paint-callback (lambda (canvas dc) (reDrawEverything dc)) ) ) ) (define (reDrawEverything dc) (draw-SetUp dc) (draw-cmdList dc cmdList '(400 250 0)) ) ;Create a pen with color red, thickness 2, ; and line type solid (define red-pen (instantiate pen% ("RED" 2 'solid)) ) ;; Show the frame (send frame show #t) ) ) (drawSystem '(f + f - f + f - f + f)) (drawSystem '(f - f - f - f f + f + f + f)) (drawSystem '(f + f + f f + f f + f f f + f f f + f f f f + f f f f))