TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on September 01, 2004, 02:17:40 PM
-
Since it seems to be sloooooooow today. I want to offer up a small little challenge for everyone. (Seriously, this is a REAL easy one. One that i think everybody will understand.) So if your new or just not that good with lisp dont be discouraged at all! Ask questions and im sure someone "better" will point you in the right direction.
So, its your mission to come up with a way to test to see if a given argument is already defined as a variable.
Example:
;;; Test to see if "a" is a var
(isvariable? a)
> nil
;;; nope, i guess not. Lets make it one.
(setq a 1)
> 1
;;; now lets test to see if its a variable
(isvariable? a)
> T
;;; w00t!
-
Can I try, or is it for newbies? [/code]
-
No, by all means give it a whirl!
-
I can make logical sense of it, but have absolutely no sense what kind of code would be required without spending more time than I have on it.
:)
-
Return T if it is a var nil if it is not
(defun isvariable? ( var )
(if var T nil)
)
OR
Return value if T or nil of not
(defun isvariable? (var)
var
)
How's that...
-
Maybe:
(defun isvariable (v)
(and (= (type (quote v)) 'SYM)
v))
-David
-
(defun isvariable (sym)
(boundp 'sym)
)
although it returns the value of sym, you can still test the return. does that count. :D
(defun isvariable (sym)
(eval 'sym)
)
-
Cool.
Ah! I didnt even think of "Boundp" I was using "null" ...lol!
;;; variable-p
;;; Test to see if item is a variable or not.
;;; Returns - a boole value
(defun variable-p (a) (not (null a)))
-
I can make logical sense of it, but have absolutely no sense what kind of code would be required without spending more time than I have on it.
Well now that people have posted some code, Do you have any questions or antything? -e.g. How in dahell dose keith's code know what to return. OR what is David D Bethel doing in his code.
Did you run any of these code examples? Do you understand them all? Which is the easiest to understand/read?
-
If you only want nil or T returned perhaps this :
(defun isvariable? (var)
(or var)
)