I would like to be able to test whether a variable is defined, prior to accessing it.
I like to have a global that specifies a "debug level". If debug level is 0, no extra output is given. When greater than 1, debug output is given, with more verbosity at greater numbers.
I also would like to set it up so that the procedures would run, and assume a level 0, if I had not gotten around to defining it. Something like: (where defined?
is the magic I don't know how to do?
(if (and (defined? debug-level) (> debug-level 1))
(diplay "Some debugging info"))
I have looked through the summary of forms in The Scheme Programming Language, 4th Edition. The only one that I saw as a possibility was identifier?
. It did not work.
I'm using SISC 1.16.6 (claims R5RS compliance) and Chez Petite Scheme v8 (claims R6RS compliance)
EDIT I tried wrapping eval
with a guard
like:
(guard (x (else #f)) (eval 'debug-level))
Since 'debug-level
is quoted it can be evaluated and passed to eval
. Then when eval
tries to evaluate it, an error would happen, which I hoped guard
would catch. It didn't.
EDIT 2 I realized that I wanted to wrap the debug tracing into a seperate procedure and that the file that defines that procedure can also define debug-level
with a default of 0. The reasons for using a seperate procedure are to lower the number of lines in the procedures that do work and also to allow the redirection of debug output if needed.
Clunky but workable solution for R5RS. Use the often neglect/forgotten ability of let-syntax to redefine keywords. this is clunky because your whole file is wrapped in a let-syntax and because it adds some overhead to each define. I use a associative list to remember definitions, a hash table would be a beter choice.
prints
Below in racket: a module is used to redefine define to store each symbol and definition in a list called define-list. The macro defined? looks in this list to see if weather or not the symbol has been defined.
In guile it is just defined? apparently: http://www.delorie.com/gnu/docs/guile/guile_289.html
This is totally up to the implementation to provide, and it looks like most implementations don't satisfactorily provide it.
In SISC scheme, it looks like you can use GETPROP to this effect,
but environments don't update automaticallyoh, look, there's this thing called INTERACTION-ENVIRONMENT that you can use:But it only works on the top level.
For Chez you have TOP-LEVEL-BOUND? and INTERACTION-ENVIRONMENT again.
To back up a bit, the problem with a
defined?
function is that if you writeScheme will attempt to evaluate
debug-level
, which of course is an error since it is not defined. Such a form would have to be implemented internally by the compiler/interpreter as a special case.Such a special form is not part of the R5RS standard (unless I missed it, please double-check). So with regard to R5RS schemes you are out of luck unless you find a Scheme that implements this as a non-standard extension.