Subject: Re: procedure-defined?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 2000/05/02
Newsgroups: comp.lang.scheme
Message-ID: <8elmnj$652ho$1@fido.engr.sgi.com>
Colin Walters  <levanti@verbum.org> wrote:
+---------------
| "John" == John Clonts <johncc@my-deja.com> writes:
| John> How can you determine if a procedure is defined?
| 
| I don't believe you can in straight R5RS.  However, guile has procedure?.
+---------------

All R[45]RS Schemes have "procedure?", but that's not the issue. That is,
he asked the wrong question, which should be: "How do you determine if a
variable (as represented by a symbol) has a global binding?", and indeed,
*that* question can't be answered in R[45]RS Scheme. (Once you know that
the variable is bound to some value, then you can safely ask "procedure?"
of that value...)

However, various Scheme implementations have their own proprietary versions
of that: "bound?", "defined?", etc. For example, in MzScheme:

	> (define (type-of-global-symbol x)
	    (if (symbol? x)
	      (if (defined? x)
	        (let ((v (global-defined-value x)))
	          (cond
		    ((procedure? v) 'procedure)
		    ((symbol? v) 'symbol)
		    ((number? v) 'number)
		    ((pair? v) 'pair)
		    ((vector? v) 'vector)
		    ((string? v) 'string)
		    ((boolean? v) 'boolean)
		    (else 'unknown-type)))
	        'undefined)
	      (error "Not a symbol:" x)))
	> (type-of-global-symbol 'foo)
	undefined
	> (type-of-global-symbol 'car)
	procedure
	> (type-of-global-symbol 123)
	Not a symbol: 123
	> (define bar 456)
	> (type-of-global-symbol 'bar)
	number
	> 

[Note that in SCM (and thus in Guile?), "defined?" is *syntax*,
not a procedure, and thus cannot be used in the manner shown above...]


-Rob

-----
Rob Warnock, 41L-955		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043