TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ElpanovEvgeniy on November 11, 2019, 10:45:47 AM

Title: double apostrophe
Post by: ElpanovEvgeniy on November 11, 2019, 10:45:47 AM
Hello!
How does this code work?

Code - Auto/Visual Lisp: [Select]
  1. (setq l '((1 2 3)(4 5)(6 7 8 9)))
  2. ;;>> (list (+ 1 2 3) (+ 4 5) (+ 6 7 8 9))
  3.  
  4.  
  5. (mapcar ''((x) (apply '+ x)) l)

My student wrote it, but I don’t understand this syntax...
Title: Re: double apostrophe
Post by: Grrr1337 on November 11, 2019, 12:19:27 PM
Hello,
Here are some self-explanations of mine (https://www.cadtutor.net/forum/topic/65911-easier-to-recursively-build-than-recursively-deconstruct-is-it-even-doable-yes/?tab=comments#comment-542133)

Then Lee made it clear with his comment (https://www.cadtutor.net/forum/topic/65911-easier-to-recursively-build-than-recursively-deconstruct-is-it-even-doable-yes/page/2/?tab=comments#comment-542704)

For short he's mapcaring with unnamed/anonymous defun-q.
Title: Re: double apostrophe
Post by: VovKa on November 11, 2019, 12:28:44 PM
My student wrote it, but I donít understand this syntax...
let's hope this student is not reading this thread ;)
Title: Re: double apostrophe
Post by: MP on November 11, 2019, 01:06:27 PM
(setq foo '((x) (* x x)))

Assigns the structure of a function to symbol foo, albeit not optimized.

(foo 2) >> 4

(mapcar '((x) (* x x)) '(2 4 6)) >> *crash*

Doesn't work because '((x) (* x x)) when evaluated returns the structure of a function - not a quoted function. Mapcar wants a quoted function.

(mapcar 'foo '(2 4 6)) >> (4 16 36)

Is functionally the same as:

(mapcar ''((x) (* x x)) '(2 4 6)) >> (4 16 36)

Works because ''((x) (* x x)) when evaluated returns the structure of a quoted function.

Hope that was clear/helps; cheers.
Title: Re: double apostrophe
Post by: Lee Mac on November 11, 2019, 01:32:05 PM
The list structure itself is essentially the same as doing this:
Code - Auto/Visual Lisp: [Select]
  1. (mapcar (defun-q f ( x ) (apply '+ x)) l)

Since:
Code - Auto/Visual Lisp: [Select]
  1. _$ (defun-q f ( x ) (apply '+ x))
  2. F
  3. _$ f
  4. ((X) (APPLY (QUOTE +) X))

Here defun-q returns the function symbol - i.e. an unevaluated/quoted function.

The function could alternatively be defined by quoting the literal list:
Code - Auto/Visual Lisp: [Select]
  1. _$ (defun-q f ( x ) (apply '+ x))
  2. F
  3. _$ (setq g '((X) (APPLY (QUOTE +) X)))
  4. ((X) (APPLY (QUOTE +) X))
  5. _$ (equal f g)
  6. T

Then, it follows that this:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l '((1 2 3) (4 5) (6 7 8 9)))
  2. ((1 2 3) (4 5) (6 7 8 9))
  3. _$ (mapcar 'g l)
  4. (6 9 30)

Can become:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar ''((X) (APPLY (QUOTE +) X)) l)
  2. (6 9 30)
Title: Re: double apostrophe
Post by: roy_043 on November 11, 2019, 05:30:27 PM
I always think of this structure as a form 'complification'. But some programmers seem to love it.
BTW BricsCAD does not accept it.
Title: Re: double apostrophe
Post by: hanhphuc on November 11, 2019, 08:10:19 PM
I always think of this structure as a form 'complification'. But some programmers seem to love it.
BTW BricsCAD does not accept it.

hi Roy just for fun

i try put triple quotes (in line 10) still can work in ACAD
' ' ' ((x) (setq s (+ x s)) s)
triple  ' ' '  ok but double  ' '  yields error

Bridscad try using ' ' double quotes possible?
' ' ((x) (setq s (+ x s) s)

Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda (x)
  2.            (last
  3.             (eval
  4.              (cons
  5.               'cond
  6.               (mapcar
  7.                '(lambda (i)
  8.                   (cons
  9.                    (equal (nth (vl-position x l) l) i)
  10.                    (mapcar 'cons '(setq mapcar) (list '(s 0.0) (list ' ' ' ((x) (setq s (+ x s)) s) i)))
  11.                    )
  12.                   )
  13.                l
  14.                )
  15.               )
  16.              )
  17.             )
  18.            )
  19.         (setq l '((list 1 2 3) (list 4 5) (list 6 7 8 9)))
  20.         )
  21.  

Title: Re: double apostrophe
Post by: Grrr1337 on November 12, 2019, 06:39:07 AM
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda (x)
  2.            (last
  3.             (eval
  4.              (cons
  5.               'cond
  6.               (mapcar
  7.                '(lambda (i)
  8.                   (cons
  9.                    (equal (nth (vl-position x l) l) i)
  10.                    (mapcar 'cons '(setq mapcar) (list '(s 0.0) (list ' ' ' ((x) (setq s (+ x s)) s) i)))
  11.                    )
  12.                   )
  13.                l
  14.                )
  15.               )
  16.              )
  17.             )
  18.            )
  19.         (setq l '((list 1 2 3) (list 4 5) (list 6 7 8 9)))
  20.         )
  21.  

Thats quite perplexing syntax, hanhphuc  :idiot2:  :-D
Title: Re: double apostrophe
Post by: ElpanovEvgeniy on November 12, 2019, 08:36:45 AM
Hello!
Thank you all so much!
I did not know about such use of defun-q
Title: Re: double apostrophe
Post by: MP on November 12, 2019, 09:22:43 AM
I always think of this structure as a form 'complification'. But some programmers seem to love it.
BTW BricsCAD does not accept it.

I donít "love it" but the native form (i.e. raw list via setq or defun-q) of function defs is propagate-able, whereas optimized function defs (via defun) are not. I touch on it briefly here (http://www.theswamp.org/index.php?topic=53912.msg585657#msg585657). Cheers.
Title: Re: double apostrophe
Post by: MP on November 12, 2019, 09:26:39 AM
Hello!
Thank you all so much!
I did not know about such use of defun-q

Pleased if I helped illuminate in some small way and reciprocate for the buckets of grey matter flexion youíve shared during your swamp tenure. :)
Title: Re: double apostrophe
Post by: roy_043 on November 12, 2019, 04:08:01 PM
I always think of this structure as a form 'complification'. But some programmers seem to love it.
BTW BricsCAD does not accept it.

I donít "love it" but the native form (i.e. raw list via setq or defun-q) of function defs is propagate-able, whereas optimized function defs (via defun) are not. I touch on it briefly here (http://www.theswamp.org/index.php?topic=53912.msg585657#msg585657). Cheers.
If you use the double apostrophe syntax there is no symbol to propagate.
Title: Re: double apostrophe
Post by: MP on November 12, 2019, 08:00:30 PM
I was talking about propagating defuns. 
Title: Re: double apostrophe
Post by: roy_043 on November 13, 2019, 02:48:18 AM
I was talking about propagating defuns. 
I know. But the aficionados of the syntax (double apostrophe) being discussed here are not concerned with that. They are not creating a named variable (symbol).