Author Topic: AutoLisp or/and not complying to CL's  (Read 9807 times)

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
AutoLisp or/and not complying to CL's
« on: July 18, 2012, 09:51:44 AM »
OK, AL's or and and only return T or nil. According to the CLHS, this is incorrect.

This is my take on attempling a CL or / CL and:
Code - Auto/Visual Lisp: [Select]
  1. (defun cl_or (forms / now)
  2.   (while (and (not (setq now (car forms)))
  3.               (setq forms (cdr forms))))
  4.   now)
  5.  
  6. (defun cl_and (forms / )
  7.   (if (apply 'and forms) (last forms)))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: AutoLisp or/and not complying to CL's
« Reply #1 on: July 18, 2012, 09:59:40 AM »
Another for OR:

Code - Auto/Visual Lisp: [Select]
  1. (defun _or ( forms )
  2.     (cond ((car forms)) (forms (_or (cdr forms))))
  3. )

AND, not so elegant:

Code - Auto/Visual Lisp: [Select]
  1. (defun _and ( forms )
  2.     (if (and (car forms) (cdr forms)) (_and (cdr forms)) (car forms))
  3. )
« Last Edit: July 18, 2012, 10:03:55 AM by Lee Mac »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #2 on: July 18, 2012, 10:15:22 AM »
Another alternative for OR:
Code - Auto/Visual Lisp: [Select]
  1. (defun _or (forms / )
  2.   (vl-some '(lambda (item) item) forms))
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: AutoLisp or/and not complying to CL's
« Reply #3 on: July 18, 2012, 10:21:27 AM »
A version of AND which doesn't use the current AND:
Code - Auto/Visual Lisp: [Select]
  1. (defun _and  (forms)
  2.   (cond ((car forms)
  3.          (cond ((cdr forms) (_and (cdr forms)))
  4.                ((car forms))))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: AutoLisp or/and not complying to CL's
« Reply #4 on: July 18, 2012, 10:38:10 AM »
Or variation
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:or (forms)
  2.   (car (vl-remove nil forms))
  3.   )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: AutoLisp or/and not complying to CL's
« Reply #5 on: July 18, 2012, 11:04:18 AM »
Another OR:

Code - Auto/Visual Lisp: [Select]
  1. (defun _or ( forms ) (vl-some ''((x) x) forms))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #6 on: July 18, 2012, 11:11:34 AM »
Wow! Didn't know that one! So you don't actually need a lambda:
Code: [Select]
Command: ('((x) (1+ x)) 2)
3
A quote does just fine!
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: AutoLisp or/and not complying to CL's
« Reply #7 on: July 18, 2012, 11:15:19 AM »
Indeed  :-)

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoLisp or/and not complying to CL's
« Reply #8 on: July 18, 2012, 02:38:10 PM »
Wow! Didn't know that one! So you don't actually need a lambda:
Code: [Select]
Command: ('((x) (1+ x)) 2)
3
A quote does just fine!

Awesome.  :mrgreen:
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoLisp or/and not complying to CL's
« Reply #9 on: July 18, 2012, 02:47:28 PM »
[OffTopic]

... Wait a minute.

Are you guys seeing the same differences in speed, or am I missing something?  :?

Code - Auto/Visual Lisp: [Select]
  1. (defun lambda1 ()
  2.   ('((acDoc)
  3.      (vla-get-name acDoc)
  4.     )
  5.   )
  6. )
  7.  
  8. (defun lambda2 ()
  9.   ((lambda (acDoc)
  10.      (vla-get-name acDoc)
  11.    )
  12.   )
  13. )
  14.  
  15. (defun lambda3 ()
  16.   (car
  17.     (mapcar
  18.       (function
  19.         (lambda (acDoc)
  20.           (vla-get-name acDoc)
  21.         )
  22.       )
  23.     )
  24.   )
  25. )
  26.  
  27. (bench '(lambda1 lambda2 lambda3) '() 100000)
  28.  

Benchmark result:
Code - Auto/Visual Lisp: [Select]
  1. LAMBDA1
  2. Elapsed: 9641
  3. Average: 0.0964
  4.  
  5. LAMBDA2
  6. Elapsed: 5101
  7. Average: 0.051
  8.  
  9. LAMBDA3
  10. Elapsed: 5974
  11. Average: 0.0597
  12.  

[/OffTopic]
« Last Edit: July 18, 2012, 02:53:17 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: AutoLisp or/and not complying to CL's
« Reply #10 on: July 18, 2012, 02:55:21 PM »
Without testing, I don't doubt that the quoted format of the lambda function will be slower since without the use of a lambda function intently expressed there is more work for the LISP interpreter to interpret the list structure as a lambda function; this behaviour is similar to the use of the function function in place of a quote.

BlackBox

  • King Gator
  • Posts: 3770
Re: AutoLisp or/and not complying to CL's
« Reply #11 on: July 18, 2012, 02:56:45 PM »
Thanks for the clarification, Lee... Sorry for the tangent, Irne  :lol:
"How we think determines what we do, and what we do determines what we get."

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: AutoLisp or/and not complying to CL's
« Reply #12 on: July 18, 2012, 11:40:18 PM »
To analyze AND / OR you can use code like this, sometimes this approach is very useful!
Code - Auto/Visual Lisp: [Select]
  1. (apply '+ (mapcar '(lambda (a) (if a 1 0)) l))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #13 on: July 19, 2012, 03:28:50 AM »
Never mind the inability to define default / infinite arguments - causing the call to have to pass all forms inside one list!

What I still can't figure out how to accomplish is to stop pre-evaluation of arguments. You'll notice the CLHS defines and/or as macros instead of functions. That means none of the forms are evaluated prior to and/or is called - then they're only called one at a time until the boolean logic causes the macro to stop.

Therefore you can do something like this for global values:
Code - Auto/Visual Lisp: [Select]
In current AL that won't only not give the correct result, but would have no efficiency gain (since both the forms are evaluated prior to passing it to _or).

You could of course quote the forms individually, but then it's breaking from what normal lisp coders would expect.

I suppose one could do something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun _or  (forms /)
  2.   (vl-some '(lambda (item) (eval item)) forms))
  3.  
  4. (defun _and  (forms / result)
  5.   (vl-every '(lambda (item) (setq result (eval item))) forms)
  6.   result)
But that means to use it you need to call it thus:
Code - Auto/Visual Lisp: [Select]
In which case, you loose the elegance of your code in order to jump through the limitation hoops of AL.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AutoLisp or/and not complying to CL's
« Reply #14 on: July 19, 2012, 04:44:19 AM »
< ..>
Code - Auto/Visual Lisp: [Select]
In current AL that won't only not give the correct result, but would have no efficiency gain (since both the forms are evaluated prior to passing it to _or).


Personally I see nothing wrong with the current AutiLisp functionality.
I don't understand why you'd want to make it look like CommonLisp

This is perfectly clear and functional
Code - Auto/Visual Lisp: [Select]
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.