TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on September 01, 2004, 02:17:40 PM

Title: (challenge) Is it a variable already?
Post 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!
Title: (challenge) Is it a variable already?
Post by: Ron Heigh on September 01, 2004, 02:45:39 PM
Can I try, or is it for newbies?   [/code]
Title: (challenge) Is it a variable already?
Post by: JohnK on September 01, 2004, 02:46:14 PM
No, by all means give it a whirl!
Title: (challenge) Is it a variable already?
Post by: M-dub on September 01, 2004, 02:49:13 PM
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.


:)
Title: (challenge) Is it a variable already?
Post by: Keith™ on September 01, 2004, 02:52:21 PM
Return T if it is a var nil if it is not
Code: [Select]

(defun isvariable? ( var )
 (if var T nil)
)


OR

Return value if T or nil of not
Code: [Select]

(defun isvariable? (var)
 var
)


How's that...
Title: (challenge) Is it a variable already?
Post by: David Bethel on September 01, 2004, 03:05:06 PM
Maybe:


Code: [Select]

(defun isvariable (v)
  (and  (= (type (quote v)) 'SYM)
            v))


-David
Title: (challenge) Is it a variable already?
Post by: Mark on September 01, 2004, 03:26:09 PM
Code: [Select]

(defun isvariable (sym)
  (boundp 'sym)
  )


although it returns the value of sym, you can still test the return. does that count. :D
Code: [Select]

(defun isvariable (sym)
  (eval 'sym)
  )
Title: (challenge) Is it a variable already?
Post by: JohnK on September 01, 2004, 03:31:14 PM
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)))
Title: (challenge) Is it a variable already?
Post by: JohnK on September 01, 2004, 03:58:15 PM
Quote from: M-dub
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?
Title: (challenge) Is it a variable already?
Post by: Kerry on September 01, 2004, 05:02:49 PM
If you only want nil or T returned perhaps this :
Code: [Select]

(defun isvariable? (var)
 (or var)
)