(define plotter
  (lambda (fx res x1 x2 y1 y2)
    (let* ((dx (- x2 x1)) (dy (- y2 y1)) (delta (/ dx res))
       (fstr (format "~a" fx))
	   (f (eval (list 'lambda '(x) fx))))
      (letrec
	  ((scaled
	    (lambda (f x y) 
	      (f
	       (* res (/ (- x x1) dx))
	       (* res (/ (- y y1) dy)))))
       (scaled-d
	    (lambda (f x y xd yd) 
	      (f
	       (+ xd (* res (/ (- x x1) dx)))
	       (+ yd (* res (/ (- y y1) dy))))))
	   (plotit
	    (lambda (x)
	      (scaled draw-line x (f x))
	      (if (< x x2) (plotit (+ x delta))))))
	(draw-color 0 0 0)
	(draw-font "Helvetica" 12)
	(scaled draw-move 0 (* 1.1 y2)) (draw-string "y")
	(scaled draw-move (* 1.1 x2) 0) (draw-string "x")
	(draw-move 
	  (- (/ res 2) 
	     (/ (car (string-size fstr "Helvetica" 12)) 2))
	  (+ 30 res)) 
	(draw-string (format "~a" fstr))
	(scaled draw-move 0 y1)
	(scaled draw-line 0 y2)
	(scaled-d draw-move 0 y2 -5 -7)
	(scaled draw-line 0 y2)
	(scaled-d draw-move 0 y2 +5 -7)
	(scaled draw-line 0 y2)
	(scaled draw-move x1 0)
	(scaled draw-line x2 0)
	(scaled-d draw-move x2 0 -7 -5)
	(scaled draw-line x2 0)
	(scaled-d draw-move x2 0 -7 +5)
	(scaled draw-line x2 0)	
	(draw-color 255 0 0)
	(scaled draw-move x1 (f x1))
	(plotit x1)))))

(plotter '(* x x x) 70 -5.0 5.0 -50.0 50.0)
(plotter '(sin x) 50 -5.0 5.0 -1.0 1.0)
(plotter '(* x (sin x)) 100 -25.0 25.0 -25.0 25.0)
(plotter '(+ (* x x) (* -5 x) 6) 80 -1.0 5.0 -3.0 10.0)
