Author Topic: (if (and  (Read 3880 times)

0 Members and 1 Guest are viewing this topic.

AWW

  • Guest
(if (and
« on: December 08, 2011, 02:27:19 PM »
Analyzing conditionals. This is what I know, pulled from older posts:

Code: [Select]
(if (this is true)
  (then do this)
)

Code: [Select]
(if (this is true)
  (then do this)
  (else do this when false)
)

Code: [Select]
(and (a. is true) ; keep going
      (b. do this & if true keep going)
      (c. do this if b was true)
)

My question is:

Code: [Select]
(if (and (XX YY ZZ)
How does it work with 2 conditionals like so? Considering if returns a value & and returns T/NIL...

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: (if (and
« Reply #1 on: December 08, 2011, 02:29:31 PM »
I would opt for the COND function instead of IF.

http://www.afralisp.net/autolisp/tutorials/cond-vs-if.php
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (if (and
« Reply #2 on: December 08, 2011, 02:49:34 PM »
My question is:

Code: [Select]
(if (and (XX YY ZZ)
How does it work with 2 conditionals like so? Considering if returns a value & and returns T/NIL...

*blink* What is the question? Sorry, my coffee blend is weak today.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

AWW

  • Guest
Re: (if (and
« Reply #3 on: December 08, 2011, 02:54:39 PM »
This is the original code I'm looking at:

Code: [Select]
  (if (and Ltext DXLeaderHandle DXItemHandle)
    (DLEADERPUTXDATA
      Ltext
      "DLEADER_TEXT"
      (vl-prin1-to-string (list DXLeaderHandle DXItemHandle))
    )
  )

The question resulted from not quite understanding whats happening with (2) conditional functions: if & and. So is the test expression evaluated twice? Once to return a T/NIL and once to return a value? Why are there 2 conditionals? Just looking for an elaboration of the code, if possible.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: (if (and
« Reply #4 on: December 08, 2011, 02:56:43 PM »
and is just checking to make sure each item is not nil. Thus returning T (all not nil) or nil (some or all are nil).

eg.
Code: [Select]
(if (and (setq a (getint "Gimmie first integer: ")) ; if not nil, continue
         (setq b (getint "Gimmie second integer: ")) ; if not nil, continue
         (setq c (getint "Gimmie third integer: ")) ; if not nil, continue
    ) ; everything not nil, do something
  (alert (strcat "First:  "
                 (itoa a)
                 "\nSecond: "
                 (itoa b)
                 "\nThird:  "
                 (itoa c)
         )
  )
  (alert "You suck!")
)
« Last Edit: December 08, 2011, 03:03:03 PM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (if (and
« Reply #5 on: December 08, 2011, 04:58:15 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: (if (and
« Reply #6 on: December 08, 2011, 05:27:09 PM »
The question resulted from not quite understanding whats happening with (2) conditional functions: if & and.

AND is not a conditional. AND is a boolean function returning the logical AND operation of one or more expressions.

AND will return T if (and only if) all of the supplied expressions evaluate to a non-nil* value.

In your example you have the structure:

Code: [Select]
(if
    (and
        <expression1>
        <expression2>
        ...
        <expressionN>
    )
    (do_this)
    (else_do_this)
)

Here, the 'test expression' is:

Code: [Select]
(and <expression1> <expression2> ... <expressionN>)
Hence, each of <expression1> ... <expressionN> must return a non-nil value for the AND function to return T, and hence for the test expression to be validated and the THEN expression to be subsequently evaluated.

This reads:

Code: [Select]
If (this AND this AND this AND ... AND this)
    do_this
    else_do_this

A possible (and far less readable) re-write could be:

Code: [Select]
(if <expression1>
    (if <expression2>
        (if <expression3>
            ...
            (if <expressionN>
                (do_this)
                (else_do_this)
            )
            ...
        )
    )
)

However, apart from being extremely difficult to manage and maintain, the intent is far harder to ascertain.

[ *Note that expressions needn't evaluate explicitely to T, but anything non-nil, this could be a string, number, list (non-empty), etc, or even T. ]

AWW

  • Guest
Re: (if (and
« Reply #7 on: December 08, 2011, 08:19:46 PM »
I see how they're tied together now. Makes sense. Will have to look into boolean functions more. Wikipedia seems to have a wealth of information on this stuff!

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: (if (and
« Reply #8 on: December 09, 2011, 04:07:44 AM »
I see how they're tied together now. Makes sense. Will have to look into boolean functions more. Wikipedia seems to have a wealth of information on this stuff!

Good stuff  8-)

Yeah, don't be 'swamped' by Wikipedia... it goes pretty deep.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: (if (and
« Reply #9 on: December 09, 2011, 08:36:27 AM »
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: (if (and
« Reply #10 on: December 09, 2011, 08:39:48 AM »
 :-P

ribarm

  • Gator
  • Posts: 3309
  • Marko Ribar, architect
Re: (if (and
« Reply #11 on: December 09, 2011, 08:59:01 AM »
Still, even now I think I couldn't wrote this code differently without (if ... (if ... (if ... )))... So please examine and make your own conclusion...
Posted on AUGI

M.R. :mrgreen:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: (if (and
« Reply #12 on: December 09, 2011, 09:22:44 AM »
Still, even now I think I couldn't wrote this code differently without (if ... (if ... (if ... )))... So please examine and make your own conclusion...
Posted on AUGI

M.R. :mrgreen:

COND

ribarm

  • Gator
  • Posts: 3309
  • Marko Ribar, architect
Re: (if (and
« Reply #13 on: December 09, 2011, 09:36:04 AM »
Still, even now I think I couldn't wrote this code differently without (if ... (if ... (if ... )))... So please examine and make your own conclusion...
Posted on AUGI

M.R. :mrgreen:

COND

That was I thinking at first glance, but try to realize it with cond... (you may reply on AUGI if you wish)
M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (if (and
« Reply #14 on: December 09, 2011, 01:07:45 PM »
I took a shot at recoding the (If's but did no testing.
Code: [Select]
(defun c:dc_block (/ _name ss ent att attpt pt0 pt1 pt2 pt3 pt4 pt5 pt6 data)
  (vl-load-com)
  (defun _name (obj)
    (if (vlax-property-available-p obj 'effectivename)
      (vla-get-effectivename obj)
      (vla-get-name obj)
    )
  )

 (defun attchk (att)
   (setq attpt (cdr (assoc 10 (entget att))))
   (setq pt0 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (/ (cdr (assoc 40 (entget att))) 2.5) 0.0)))
   (setq pt1 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (/ (cdr (assoc 40 (entget att))) 6.0) 0.0)))
   (setq pt2 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (* (/ (cdr (assoc 40 (entget att))) 6.0) 5.0) 0.0)))
   (setq pt3 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (- (/ (cdr (assoc 40 (entget att))) 10.0)) 0.0)))
   (setq pt4 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (cdr (assoc 40 (entget att))) 0.0)))
   (setq pt5 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (/ (cdr (assoc 40 (entget att))) 2.0) 0.0)))
   (setq pt6 (mapcar '+ attpt (list (/ (cdr (assoc 40 (entget att))) 6.0) (- (/ (cdr (assoc 40 (entget att))) 3.0)) 0.0)))
  )
 
  (if (and (or (and (setq ss (ssget "_I" '((0 . "INSERT") (66 . 1))))
                    (setq ent (ssname ss 0))
                    (setq att (entnext ent))
               )
               (and (setq ent (car (entsel "\nSelect block: ")))
                    (setq att (entnext ent))
                    (eq (cdr (assoc 0 (setq data (entget ent)))) "INSERT")
                    (eq (cdr (assoc 66 data)) 1)
               )
           )
           (eq (_name (vlax-ename->vla-object ent)) "24x36 Old")
      )
    (progn
      (command "_.eattedit" ent)
      (while (eq (cdr (assoc 0 (entget att))) "ATTRIB")
        (attchk att)
        (vl-some (function (lambda (p) (if (ssget p) (command "_.attipedit" p))) )
                 (list attpt pt0 pt1 pt2 pt3 pt4 pt5 pt6)
        )
        (setq att (entnext att))
      )
    )
  )
  (print (_name (vlax-ename->vla-object ent)))
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.