Autolisp Return boolean

Autolisp Return boolean

GeryKnee
Advocate Advocate
387 Views
2 Replies
Message 1 of 3

Autolisp Return boolean

GeryKnee
Advocate
Advocate

Hi,

 

Trying to get a boolean return 

I wrote the following :

 

(defun fnTestBooleanEqualTo1 ( Value / Ret)
(if (= value 1)
(Setq Ret T)
(Setq Ret nil)
)
)


(defun c:xx ( / )
(if (fnTestBooleanEqualTo1 0)
(princ "\n0 0 =1 ")
(princ "\n0 0<>1")
)
(if (fnTestBooleanEqualTo1 1)
(princ "\n0 1 =1 ")
(princ "\n0 1<>1")
)
(princ)
)

 

It's working but ...

I imagene there's a more elegant road for this

Maybe it's something simple.

 

Thanks,

Gery

0 Likes
Accepted solutions (1)
388 Views
2 Replies
Replies (2)
Message 2 of 3

ronjonp
Mentor
Mentor

@GeryKnee 

Your first code could be as simple as this:

(defun fntestbooleanequalto1 (value) (= value 1))

 

What are you trying to accomplish?

 

Really for something like this you can check directly (= value 1)

0 Likes
Message 3 of 3

martti.halminen
Collaborator
Collaborator
Accepted solution

The first thing Lisp newcomers struggle with regarding booleans: there is no separate boolean data type, NIL is considered false, anything else whatsoever is considered true.

 

- There is the convention of using T as the canonical truth value if you need to create an answer, but that is just to make life easier for the human reader. The program would work equally well with 0, 42, 999999, 'NO, '#False, "Mickey Mouse", #<USUBR @00000167a756acc8 ONEP>, "" or whatever.

 

When thinking about elegance, remember that Lisp originated as a functional programming language, so anything you call returns a result. It is often simpler to use the result instead of storing stuff into variables.

 

So,

 

(if (= value 1)
   (Setq Ret T)
   (Setq Ret nil))

 

could be said simpler as

 

(if (= value 1)

     T

     nil)

 

And even more simply as

 

(= value 1)