Code Red > AutoLISP (Vanilla / Visual)
double apostrophe
ElpanovEvgeniy:
Hello!
How does this code work?
--- Code - Auto/Visual Lisp: ---(setq l '((1 2 3)(4 5)(6 7 8 9)));;>> (list (+ 1 2 3) (+ 4 5) (+ 6 7 8 9)) (mapcar ''((x) (apply '+ x)) l)
My student wrote it, but I don’t understand this syntax...
Grrr1337:
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.
VovKa:
--- Quote from: ElpanovEvgeniy on November 11, 2019, 10:45:47 AM ---My student wrote it, but I don’t understand this syntax...
--- End quote ---
let's hope this student is not reading this thread ;)
MP:
(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.
Lee Mac:
The list structure itself is essentially the same as doing this:
--- Code - Auto/Visual Lisp: ---(mapcar (defun-q f ( x ) (apply '+ x)) l)
Since:
--- Code - Auto/Visual Lisp: ---_$ (defun-q f ( x ) (apply '+ x))F_$ f((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: ---_$ (defun-q f ( x ) (apply '+ x))F_$ (setq g '((X) (APPLY (QUOTE +) X)))((X) (APPLY (QUOTE +) X))_$ (equal f g)T
Then, it follows that this:
--- Code - Auto/Visual Lisp: ---_$ (setq l '((1 2 3) (4 5) (6 7 8 9)))((1 2 3) (4 5) (6 7 8 9))_$ (mapcar 'g l)(6 9 30)
Can become:
--- Code - Auto/Visual Lisp: ---_$ (mapcar ''((X) (APPLY (QUOTE +) X)) l)(6 9 30)
Navigation
[0] Message Index
[#] Next page
Go to full version