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

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #15 on: July 19, 2012, 05:36:37 AM »
That's just used as a sample ... not a particularly good one at that. Your code would actually be ever so slightly more efficient than the forced CL example - since the setq only happens if needed.

A better example might be where you'd like to set a variable's value to something only if a set of conditions are met:
Code - Auto/Visual Lisp: [Select]
  1. ;; AutoLisp code
  2. (setq result (if (and cond1 cond2 cond3 cond4) value))
  3. ;; Common Lisp version
  4. (setq result (and cond1 cond2 cond3 cond4 value))
Another sample might be that you want the 1st item which is not nil as well as some other value:
Code - Auto/Visual Lisp: [Select]
  1. ;; AutoLisp Version
  2. (setq result (cond ((setq tmp (eval cond1)) (setq result2 1) tmp)
  3.                    ((setq tmp (eval cond2)) (setq result2 2) tmp)
  4.                    ((setq tmp (eval cond3)) (setq result2 3) tmp)
  5.                    ((setq tmp (eval cond4)) (setq result2 4) tmp)
  6.                    (t (setq result2 0) nil)))
  7. ;; Common Lisp version
  8. (setq result (or (and (setq result2 0) (eval cond1))
  9.                  (and (setq result2 1) (eval cond2))
  10.                  (and (setq result2 2) (eval cond3))
  11.                  (and (setq result2 3) (eval cond4))
  12.                  (and (setq result2 4) nil)))
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 #16 on: July 19, 2012, 05:47:45 AM »

It would drive me crazy if AND and OR did not return a boolean value.  ...

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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: AutoLisp or/and not complying to CL's
« Reply #17 on: July 19, 2012, 05:49:55 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun _or (l) (eval (cons 'cond (mapcar 'list l))))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #18 on: July 19, 2012, 06:04:11 AM »

It would drive me crazy if AND and OR did not return a boolean value.  ...
Huh? In lisp everything /= nil is evaluated as "not-false". There is no such thing as "true", there's only "not-false" ... or rather "not-nothing". The T symbol is only added as a constant for special cases: http://www.lispworks.com/documentation/HyperSpec/Body/v_t.htm Effectively it's not needed.

Code - Auto/Visual Lisp: [Select]
  1. (defun _or (l) (eval (cons 'cond (mapcar 'list l))))
Good one! That's probably how it would be implemented inside CL!
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 #19 on: July 19, 2012, 06:15:23 AM »

I mean boolean having two possible values representing “true” or “false.”
... not ANY other value

AND and OR by 'logical' definition return a boolean value.
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: AutoLisp or/and not complying to CL's
« Reply #20 on: July 19, 2012, 07:11:11 AM »
That may be true in other languages, but lisp works differently. In lisp everything is considered "true" unless it is nil. E.g.
Code - Auto/Visual Lisp: [Select]
  1. (if value
  2.   (princ "Tue")
  3.   (princ "False"))
If value=100, then above would print "True". If value=(1 3 5 2 76), then it would print "True". If value=T then it would print "True". The only time when it would EVER print "False" is when value=nil.

Even consider this:
Code - Auto/Visual Lisp: [Select]
  1. $ (setq value 'nil)
  2. nil
  3. _$ (if value "True" "False")
  4. "False"
  5. _$ (if 'value "True" "False")
  6. "True"
So even a reference to the value containing nil is still considered True. It's only when the actual value being checked results in nil that a False is signaled.

The CL versions of the logical operators are defined to reflect this very concept instead of attempting to enforce some unnatural rule that only True /= False. It's one of the major differences between Lisp and most other languages. In lisp you can see such as analogous to an if asking: "Is the test resulting in something?"

For this reason you don't need to check specifics like you need to in other languages. E.g. say you're checking if an argument was passed in C# with an instantiated object, you'd need to do this:
Code - C#: [Select]
  1. if (arg != nothing)
  2. { // Continue with true part
  3. }
  4. else { // Continue with false part
  5. }
In Lisp all you need to do is:
Code - Auto/Visual Lisp: [Select]
  1. (if arg
  2.    (progn ;; Continue with true part
  3.    )
  4.    (progn ;; Continue with false part
  5.    )
  6. )
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 #21 on: July 19, 2012, 07:22:28 AM »
If lisp worked as per your idea, then the logical operators should also only allow T or nil as input arguments. E.g. instead of:
Code - Auto/Visual Lisp: [Select]
  1. (or nil 1 3 6 nil)
You'd then have to write:
Code - Auto/Visual Lisp: [Select]
  1. (or (not (eq nil nil)) (not (eq 1 nil)) (not (eq 2 nil)) (not (eq 6 nil)) (not (eq nil nil)))
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 #22 on: July 19, 2012, 07:40:12 AM »
irneb

I've been playing with Lisp since before you were in nappies, I don't need language lessons, but thanks.

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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: AutoLisp or/and not complying to CL's
« Reply #23 on: July 19, 2012, 08:33:20 AM »
You know Kerry, if you stop using the (or *var* (setq *var* ... syntax people might not get confused. :P

Hi Kerry, It's been awhile. How have you been?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: AutoLisp or/and not complying to CL's
« Reply #24 on: July 19, 2012, 08:35:45 AM »
Why Common Lisp? CL and AL are so different. Scheme would be a slightly better reference.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #25 on: July 19, 2012, 10:11:52 AM »
I've been playing with Lisp since before you were in nappies, I don't need language lessons, but thanks.
Then I'm a bit confused as to why you're questioning this. So you were playing with lisp since the 70's? I tip my hat, I was only starting with it in the early 90's. Hope I didn't offend - it just appeared that you were under a misconception about how lisp handled "booleans". It wouldn't be the first time I've misread someone's intention in a post  :ugly:

Why Common Lisp? CL and AL are so different. Scheme would be a slightly better reference.
Even in scheme the or & and work this way. See page 26 here: http://groups.csail.mit.edu/mac/ftpdir/mit-scheme/7.7/7.7.1/doc-pdf/scheme.pdf

About SCM being closer to AL, that's debatable. The syntax is much the same, but concepts such as Lexical/Dynamic scope (in SCM Lex is enforced, in CL it's optional, in AL it's impossible). AL uses the same value for falsity, failure & empty list - i.e. nil, SCM uses different constants for each. Even the some of the basic "standard" functions are named differently in SCM, e.g. defun is in CL, but the equivalent SCM would be a define of a lambda.

AFAICT the only point where AL is more like SMC is that it uses a single namespace, whereas CL uses separate namespaces for functions and variables (i.e. a function and a variable may have the same name; also if a function is passed as an argument for callback you need to prefix with #').  http://c2.com/cgi/wiki?LispSchemeDifferences

Perhaps I'm missing something, could you point me in some direction as to why SCM is a closer match to AL?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: AutoLisp or/and not complying to CL's
« Reply #26 on: July 19, 2012, 10:30:31 AM »
I do not understand you!
Do you want to say that the bad language AutoLISP, compared to others? As far as I know, all languages ​​are created on the basic functions, and then appended to the inside. What's stopping you from creating the necessary functions for your convenience?

Here, gathered followers of LISP. We love him for who he is. If necessary, we add new features in LISP or use other languages​​...

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #27 on: July 19, 2012, 10:53:04 AM »
Not sure you were posting in response to me. If so I definitely don't think AutoLisp is a BAD language, I just think it's lost ground on other lisps - and would like to make it more capable. I'd actually like to stay with lisp instead of mixing different languages for my programs, but I'm constantly running into situations where AL/VL simply cannot do it - and then noticing that CL/SCM could have.

If it's about the which is closer SCM/CL ... then I'm simply looking for a place where these "extensions" I'm attempting is already defined. Rather than reinventing stuff.
« Last Edit: July 19, 2012, 10:56:24 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: AutoLisp or/and not complying to CL's
« Reply #28 on: July 19, 2012, 11:09:51 AM »
...could you point me in some direction as to why SCM is a closer match to AL?

*Pthht*, beats me?! I haven't done LISP in several years now, and when I did it was more academic in nature (I did what you are doing now; create LET, understand EVAL, create an AL Huffman encoding function, etc.). CL and I never got along but I did start to read "ON-LISP" (I got side-tracked with the paragraph on "hygienic variables" and never went back though). I only said `slightly better' because I kinda remembered discussion(s) between several of the older gods-of-Autolisp. -i.e. When I wanted to learn Autolisp I was told by those `old-hands' to read SICP (Scheme) so I did. My thought was that since this exercise, and the Scheme language, seems more skewed towards the academic side...
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: AutoLisp or/and not complying to CL's
« Reply #29 on: July 20, 2012, 03:12:19 AM »
Thanks Se7en, I do remember a thread mentioning something about AL being closer to SCM than CL (just don't remember where it was). But I've investigated both myself, and looking at sample code for both I get the feeling that CL looks more familiar to me, while most of the SCM stuff looks very foreign (in comparison to AL that is). If anyone could give me a conclusive answer to this I'd swap to SCM anytime, but thus far I just don't see it.

In any case, for the purposes of this thread SCM/CL is a moot point - since both set the definitions for and/or the same way. Even the original lisp ("daddy") of AL (XLisp) has the or/and defined this way: http://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/xlisp/xlisp-ref/xlisp-ref-192.htm

Though I can't find a reference for XLisp 1.0's (the one from which AL was derived) or/and definitions. So possibly this change happened in the 2.0 version - which also includes a lot of CL's additions (most notably objects & classes). But I wouldn't be surprised if it was one of the changes made by Adesk in order to get the original AL to compiled size <64kbi.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.