Author Topic: (challenge) Is it a variable already?  (Read 3576 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10635
(challenge) Is it a variable already?
« 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!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Ron Heigh

  • Guest
(challenge) Is it a variable already?
« Reply #1 on: September 01, 2004, 02:45:39 PM »
Can I try, or is it for newbies?   [/code]

JohnK

  • Administrator
  • Seagull
  • Posts: 10635
(challenge) Is it a variable already?
« Reply #2 on: September 01, 2004, 02:46:14 PM »
No, by all means give it a whirl!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

M-dub

  • Guest
(challenge) Is it a variable already?
« Reply #3 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.


:)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) Is it a variable already?
« Reply #4 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...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

David Bethel

  • Swamp Rat
  • Posts: 656
(challenge) Is it a variable already?
« Reply #5 on: September 01, 2004, 03:05:06 PM »
Maybe:


Code: [Select]

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


-David
R12 Dos - A2K

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) Is it a variable already?
« Reply #6 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)
  )
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10635
(challenge) Is it a variable already?
« Reply #7 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)))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10635
(challenge) Is it a variable already?
« Reply #8 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?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
(challenge) Is it a variable already?
« Reply #9 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)
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.