Author Topic: LISP equality test gotcha  (Read 6925 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
LISP equality test gotcha
« on: April 05, 2010, 12:30:12 PM »
Had a bug in one of my programs (introduced by a quick edit I had done this morning). Fortunately I found it and fixed it quick but in some circumstances it could be a bugger to trace the following (seemingly) illogical behavior.

<clears throat> ahem (symbolically):

Code: [Select]
(setq
    var1 nil
    var2 0
)

(< var1 var2)

In a strongly typed language this would throw an uninitialized variable error.

Not in LISP. It happily returns T (or nil if you reverse the terms).

Buyer beware.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: LISP equality test gotcha
« Reply #1 on: April 05, 2010, 12:37:35 PM »
Wow, thanks.
Now if I can only remember that for future use.  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.

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: LISP equality test gotcha
« Reply #2 on: April 05, 2010, 12:41:07 PM »
Thanks Michael :-)  Good to know.

While we're on the topic, I discovered this recently...

Code: [Select]
(setq l1 '(1 2) l2 '(1 2))

(eq l1 l2)  ==>  nil

(= l1 l2)  ==>  nil

(equal l1 l2)  ==>  T

Similarly:

Code: [Select]
e1
<Entity name: 7ef03798>

e2
<Entity name: 7ef03798>

(eq e1 e2)  ==>  T

(= e1 e2)  ==>  nil

(equal e1 e2)  ==>  T

I'm sure there are many other examples....

T.Willey

  • Needs a day job
  • Posts: 5251
Re: LISP equality test gotcha
« Reply #3 on: April 05, 2010, 12:47:43 PM »
Lee,

The only time I use ' = ' is on strings, and I never use ' eq ', always ' equal '.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

JohnK

  • Administrator
  • Seagull
  • Posts: 10656
Re: LISP equality test gotcha
« Reply #4 on: April 05, 2010, 12:47:57 PM »
Had a bug in one of my programs (introduced by a quick edit I had done this morning). Fortunately I found it and fixed it quick but in some circumstances it could be a bugger to trace the following (seemingly) illogical behavior.

<clears throat> ahem (symbolically):
...
huh? That could be a real pain to find.

Code: [Select]
(cond
  ((and var1 var2)
    (< var1 var2))
  )
I mean, Ive never really thought to run a conditional for every equality test.

whoa? *thought* didnt we hit on this subject that one time we discussed the "anaphoiric if" (Im gonna go look up my notes)?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: LISP equality test gotcha
« Reply #5 on: April 05, 2010, 12:50:50 PM »
Code: [Select]
(< -1e+99 var1 var2)
(< nil var1 var2)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP equality test gotcha
« Reply #6 on: April 05, 2010, 02:18:07 PM »
huh? That could be a real pain to find.

Code: [Select]
(cond
  ((and var1 var2)
    (< var1 var2))
  )
I mean, Ive never really thought to run a conditional for every equality test.

And I'd never do that ^ kind of checking. The problem I had a programming error, not an execution error. If, in the course of normal execution, it was expected behavior that var1 could be assigned nil then I'd branch long before the conditional test noted. In my actual program I had commented out some code and replaced it with new code. Unfortunately I also commented out the variable assignment code, introducing the programming bug.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP equality test gotcha
« Reply #7 on: April 05, 2010, 02:20:19 PM »
On eq vs equal, generally speaking I use eq when comparing numbers or strings, otherwise equal.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: LISP equality test gotcha
« Reply #8 on: April 05, 2010, 03:16:53 PM »
On eq vs equal, generally speaking I use eq when comparing numbers or strings, otherwise equal.

I would usually use 'eq' for strings, and '=' for numbers, and otherwise equal - but, to be honest, I wouldn't normally put myself in a situation where I would compare entity names or VLA-objects.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP equality test gotcha
« Reply #9 on: April 05, 2010, 03:47:29 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: LISP equality test gotcha
« Reply #10 on: April 05, 2010, 04:01:59 PM »
Lee,
Code: [Select]
(equal l1 l2)  ==>  T
Quote
(equal l1 l2)  ==>  T
With this font it looked like eleven & twelve.  :-o
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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: LISP equality test gotcha
« Reply #11 on: April 05, 2010, 07:32:00 PM »
Lee,
Code: [Select]
(equal l1 l2)  ==>  T
Quote
(equal l1 l2)  ==>  T
With this font it looked like eleven & twelve.  :-o

At first, I thought the same thing. Needless to say, I was rather confused.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JCTER

  • Guest
Re: LISP equality test gotcha
« Reply #12 on: April 05, 2010, 07:40:32 PM »
[]http://www.karolinagames.com/horatio/images/90d31b379587cb1bf205881c9a1209ee.jpg[/img]

No offense, but... worst... use of that meme... ever.
 :-P

uncoolperson

  • Guest
Re: LISP equality test gotcha
« Reply #13 on: April 05, 2010, 07:42:41 PM »
anyone else confused about the two sets of sunglasses?

JCTER

  • Guest
Re: LISP equality test gotcha
« Reply #14 on: April 05, 2010, 07:44:55 PM »
anyone else confused about the two sets of sunglasses?
Because he's always wearing sunglasses... even when he does his dramatic putting-on of his sunglasses.  He's -that- Miami.