[plt-scheme] knowing if dynamic-require will succeed
In an earlier thread I was provided with a nice automatic way to make
my code run with the plot module and produce graphical output when I
run it on my mac in DrScheme, but run without the plot module and
without graphical output when I run it on my linux cluster with
mzscheme. The final version of this is below. It uses (equal? 'unix
(system-type)) to produce a definition that is either void or a
plotting routine that uses dynamic-require to refer to the plot and
points functions.
I now plan to share this with students, some of whom I imagine may be
running it in a graphical unix environment in which plot IS available,
or alternatively in a non-unix environment in which plot is NOT
available (like maybe in mzscheme under Windows or something like
that, I guess).
So what I would ideally like to do is to replace (equal? 'unix (system-
type)) with something that asks what I really need to know, which is
"Will dynamically requiring plot fail?"
Is there a way to do this?
I did see in the documentation for dynamic-require that there is an
optional fail-thunk argument, and I thought that that might provide
the solution. The second version of plot-data below is my attempt to
use this. But it doesn't work -- when I run it on my cluster it gives:
dynamic-require: unknown module: '#%mred-kernel; need to run in mred
instead of mzscheme
My next thoughts were that I might find a solution either in dynamic-
wind or by providing an exception handler, but both of these look
fairly complicated and I would appreciate any suggestions that any of
you have.
Thanks,
-Lee
;; works correctly but isn't really basing the decision on the right
criterion
(define plot-data
(if (equal? 'unix (system-type))
void
(lambda (data x-label y-label)
((dynamic-require 'plot 'plot)
((dynamic-require 'plot 'points) (for/list ((i (number-list
(length data))))
(vector i (list-ref data i)))
#:sym 'oplus)
#:x-min 0
#:x-max (length data)
#:y-min 0
#:y-max (+ 1 (apply max data))
#:x-label x-label
#:y-label y-label))))
;; the fail-thunk doesn't prevent an error if the plot module isn't
available
(define plot-data
(let* ((plot-module-available #t)) ;; assume it's available, but
then check
(dynamic-require 'plot 'plot (lambda () (set! plot-module-
available #f))) ;; is it really available?
(if plot-module-available
(lambda (data x-label y-label)
((dynamic-require 'plot 'plot)
((dynamic-require 'plot 'points) (for/list ((i (number-
list (length data))))
(vector i (list-ref
data i)))
#:sym 'oplus)
#:x-min 0
#:x-max (length data)
#:y-min 0
#:y-max (+ 1 (apply max data))
#:x-label x-label
#:y-label y-label))
void)))
--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438
Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/