Author Topic: double apostrophe  (Read 366 times)

CincyJeff and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
double apostrophe
« 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...
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

Grrr1337

  • Swamp Rat
  • Posts: 704
Re: double apostrophe
« Reply #1 on: November 11, 2019, 12:19:27 PM »
Hello,
Here are some self-explanations of mine

Then Lee made it clear with his comment

For short he's mapcaring with unnamed/anonymous defun-q.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

VovKa

  • Swamp Rat
  • Posts: 1155
  • Ukraine
Re: double apostrophe
« Reply #2 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 ;)

MP

  • Seagull
  • Posts: 17467
Re: double apostrophe
« Reply #3 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.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Lee Mac

  • Seagull
  • Posts: 12283
  • London, England
Re: double apostrophe
« Reply #4 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)
« Last Edit: November 11, 2019, 01:38:00 PM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1731
  • BricsCAD 18
Re: double apostrophe
« Reply #5 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.

hanhphuc

  • Newt
  • Posts: 53
Re: double apostrophe
« Reply #6 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.  

( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

Grrr1337

  • Swamp Rat
  • Posts: 704
Re: double apostrophe
« Reply #7 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
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: double apostrophe
« Reply #8 on: November 12, 2019, 08:36:45 AM »
Hello!
Thank you all so much!
I did not know about such use of defun-q
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

MP

  • Seagull
  • Posts: 17467
Re: double apostrophe
« Reply #9 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. Cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17467
Re: double apostrophe
« Reply #10 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. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

roy_043

  • Water Moccasin
  • Posts: 1731
  • BricsCAD 18
Re: double apostrophe
« Reply #11 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. Cheers.
If you use the double apostrophe syntax there is no symbol to propagate.

MP

  • Seagull
  • Posts: 17467
Re: double apostrophe
« Reply #12 on: November 12, 2019, 08:00:30 PM »
I was talking about propagating defuns. 
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

roy_043

  • Water Moccasin
  • Posts: 1731
  • BricsCAD 18
Re: double apostrophe
« Reply #13 on: Today at 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).