Author Topic: "and" versus "if"  (Read 4126 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 650
"and" versus "if"
« on: November 22, 2013, 12:18:03 PM »
I found this code
Code: [Select]
(and (= f ".") (setq Dirs (cddr Dirs)))
(foreach n Dirs
Now there is no second condition, and so I wonder why it is not:
Code: [Select]
(if (= f ".") (setq Dirs (cddr Dirs)))
(foreach n Dirs
The only difference I see is that
- 'and' returns T or nil
- 'if' returns nil or the new value of 'Dirs'
but 'foreach' does not care about it ...

So what is the advantage of 'and' in this fragment?

Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: "and" versus "if"
« Reply #1 on: November 22, 2013, 12:35:13 PM »
Not much, aside from being easier to extend if additional conditions are expected in the future.  Other than that, its more of a style choice.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

owenwengerd

  • Bull Frog
  • Posts: 451
Re: "and" versus "if"
« Reply #2 on: November 22, 2013, 01:12:30 PM »
In my opinion, (if) is better in this case because it is less likely to be misunderstood. However, I would write it more succinctly:

Code: [Select]
(foreach n (if (= f ".") (cddr Dirs) Dirs)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: "and" versus "if"
« Reply #3 on: November 22, 2013, 01:39:12 PM »
Not much, aside from being easier to extend if additional conditions are expected in the future.  Other than that, its more of a style choice.
With you on that .... though ....
In my opinion, (if) is better in this case because it is less likely to be misunderstood. However, I would write it more succinctly:

Code: [Select]
(foreach n (if (= f ".") (cddr Dirs) Dirs)
If you take Owen's post into account, a cond would probably sort out both scenarios. Easier to extend, yet still readable without special knowledge of how Lisp operates.

The only difference I see is that
- 'and' returns T or nil
- 'if' returns nil or the new value of 'Dirs'
That's actually a peculiarity of AutoLisp. In all other Lisps (at last those I've used) there would be asolutely no difference in this case. The and would return the exact same thing as the if does.
but 'foreach' does not care about it ...
Exactly. Any boolean test in Lisp (all types of Lisps) is a situation of: "Is it something else than nil? If so consider it true, else consider it false."
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: "and" versus "if"
« Reply #4 on: November 22, 2013, 02:54:32 PM »
I use AND sometimes just because it keep all the code on one line when formatted.  ;D

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Peter2

  • Swamp Rat
  • Posts: 650
Re: "and" versus "if"
« Reply #5 on: November 22, 2013, 04:53:08 PM »
Thanks to all for the interesting comments. My 2 cents for flexibility: 'If' can be modified with 'else-code', 'and' not ...

Have a fine weekend!
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: "and" versus "if"
« Reply #6 on: November 22, 2013, 07:19:23 PM »
I use AND sometimes just because it keep all the code on one line when formatted.  ;D

Ditto  8)
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: "and" versus "if"
« Reply #7 on: November 22, 2013, 08:18:36 PM »
Where's John when you want a good argument regarding if, and, or, cond.
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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: "and" versus "if"
« Reply #8 on: November 23, 2013, 06:52:22 AM »
Where's John when you want a good argument regarding if, and, or, cond.
  :lmao: ... you could always go bonkers and make the code so obtuse as to mislead even yourself. E.g. what about converting each input into integers and then using logand on them?  :-P
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: "and" versus "if"
« Reply #9 on: November 23, 2013, 07:02:49 AM »
Thanks to all for the interesting comments. My 2 cents for flexibility: 'If' can be modified with 'else-code', 'and' not ...
Yes, that's a good point too. Yet another reason cond is more flexible (it can have multiple elses without needing to nest ifs).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: "and" versus "if"
« Reply #10 on: November 23, 2013, 07:12:02 AM »
Code: [Select]
(and (= f ".") (setq Dirs (cddr Dirs)))
(foreach n Dirs
Personally in your code example I'd simply remove the current directory "." and parent directory ".." a different way. But that's just me, I don't like relying on them always being the 1st 2 ... not that I've ever found them otherwise, but I've seen too many issues happen when relying on the order of a list. I'd have done something like this instead:
Code: [Select]
(foreach n (vl-remove "." (vl-remove ".." Dirs))
  ...
)
Edit: e.g. what if the list of dirs is sorted? Could you still rely on "." and ".." to be the 1st two in the list?
« Last Edit: November 23, 2013, 07:20:00 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Peter2

  • Swamp Rat
  • Posts: 650
Re: "and" versus "if"
« Reply #11 on: November 23, 2013, 08:34:51 AM »
Edit: e.g. what if the list of dirs is sorted? Could you still rely on "." and ".." to be the 1st two in the list?
a) yes, it's sorted.
b) ... rely on ..- I think yes, because Dirs is a list of subdirs (it's not my code ...)
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: "and" versus "if"
« Reply #12 on: November 24, 2013, 11:32:24 PM »
a) yes, it's sorted.
b) ... rely on ..- I think yes, because Dirs is a list of subdirs (it's not my code ...)
Well, in most cases "." would be at the start if you sort the list. It's supposed to be at the start on a raw list (as obtained from vl-directory-files). But if one of the sub-dirs start with a "strange" character (e.g. $ or any of those allowed with an ASCII code smaller than that of ".") it would get placed before the "." and ".." when sorted.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Peter2

  • Swamp Rat
  • Posts: 650
Re: "and" versus "if"
« Reply #13 on: November 27, 2013, 02:02:47 PM »
.... But if one of the sub-dirs start with ...
Yes, thank you. That's an important argument.
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23