|
发表于 2022-7-5 14:19:35
|
显示全部楼层
Good morning all!
Well, for me there is a margin between knowing and understanding. With your examples and gile's, I can say that now I (kinda) know. I still don't really understand. Reading Hanhphuc's "Attempted Lee's 'list function' in mapcar but failed, then tried debugging using extra quote then it worked." I think he is in the same boat as me; he got there by trial/error. I understand structures like (mapcar 'lambda or (mapcar (function (lambda. I also kind of understand why (mapcar (function '((x)... doesn't work, and returns "bad FUNCTION argument", as your demonstration of its type is a LIST format. BUT, from the mapcar'S help, the first argument is supposed to be a function type Subr, and in all logic should it not bomb as well with the same error, as '((x)... is a list and not a subr? mmmm. Let's have fun and create some more confusion...^^ Let's say I take (with list 1 2 3 rather than list a b c)... like you hanh, why 1+ needs to be quoted: I know, but don't really understand. Here's why
(mapcar '1+ (list 1 2 3)), (mapcar (function 1+) (list 1 2 3)), (mapcar '1+ '(1 2 3)) etc.. both work and return the same. Mapcar'S requires a list as 2nd argument, and (type '(1 2 3)) effectively return a LIST. So far so good... Mind boggling things:
Mapcar requires a function/subr as first argument
Both (type (function 1+)) and (type '1+) returns SYM.
(type 1+) returns SUBR
(mapcar 1+ (list 1 2 3)) will error and return ; error: bad function: #
Basically, while mapcar'S argument #2 is a list, and (type '(1 2 3)) effectively return a LIST, its first argument is supposed to be a subr but (type '1+) returns SYM. But I know how mapcar works.
I never used vlx, but if my understanding is correct, both can be compiled to a vlx, the difference being that quoted lambda can be viewed somehow. I forgot the "animate" option in vlide while opening a new drawing, and a bunch of "source" windows appeared, each containing a single lambda while the whole acaddoc/startup/etc was animated.
Indirectly yes: Because of that it wasthe only one that didn't use recursion, as hanh pointed out. It also is why I said that it was an uneven apple to banana comparison.
While I'm not sure I totally agree that "function (lambda" is a more developed code, or that writing speed optimized code is more time consuming (once you know, you know, it is not like adding the word "function" requires that much time), I agree that always keeping speed in mind is an excellent practice. I don't understand what you mean by "this is somewhat neither fully coded (function) function, neither fully quoted...". Maybe the impact is bigger when it comes to lambda, and/or vlx, but if I benchmark (mapcar '1+ (list 1 2 3)) vs (mapcar (function 1+) (list 1 2 3)), I don't get a speed difference that seems very significative- Elapsed milliseconds / relative speed for 16384 iteration(s): (MAPCAR (FUNCTION 1+) (LIST 1 2 3)).....1451 / 1.02 (MAPCAR (QUOTE 1+) (LIST 1 2 3))........1482 / 1.00 Elapsed milliseconds / relative speed for 16384 iteration(s): (MAPCAR (FUNCTION 1+) (LIST 1 2 3)).....1357 / 1.01 (MAPCAR (QUOTE 1+) (LIST 1 2 3))........1373 / 1.00
I would really try hard to find better/faster ways to achieve something that requires an amount of time perceptible by the user even if it is just a "stutter" like 0.1s and more.
(mapcar (function 1+) (list 1 2 3)) is more developed/better version of (mapcar '1+ (list 1 2 3))? That example would needs to be ran 10 000 times in order to save something like 10 ms. I'm playing devils advocate here because taking for granted that function was better and faster than quoting lambdas, I had some surprises. - Command: (BenchMark '(('(_> (mapcar ''((x)(1+ x)) '(1 2 3))('(_> (mapcar '(lambda (x) (1+ x)) '(1 2 3))('(_> (mapcar (function (lambda (x) (1+ x))) '(1 2 3))('(_> ))Elapsed milliseconds / relative speed for 8192 iteration(s): (MAPCAR (QUOTE (LAMBDA (X) (1+ X))) ...).....1123 / 1.15 (MAPCAR (FUNCTION (LAMBDA (X) (1+ X)...).....1170 / 1.11 (MAPCAR (QUOTE (QUOTE ((X) (1+ X))))...).....1295 / 1.00 Elapsed milliseconds / relative speed for 16384 iteration(s): (MAPCAR (QUOTE (LAMBDA (X) (1+ X))) ...).....1716 / 1.17 (MAPCAR (FUNCTION (LAMBDA (X) (1+ X)...).....1732 / 1.16 (MAPCAR (QUOTE (QUOTE ((X) (1+ X))))...).....2013 / 1.00 Elapsed milliseconds / relative speed for 8192 iteration(s): (MAPCAR (QUOTE (LAMBDA (X) (1+ X))) ...).....1076 / 1.15 (MAPCAR (FUNCTION (LAMBDA (X) (1+ X)...).....1107 / 1.11 (MAPCAR (QUOTE (QUOTE ((X) (1+ X))))...).....1233 / 1.00 Elapsed milliseconds / relative speed for 16384 iteration(s): (MAPCAR (QUOTE (LAMBDA (X) (1+ X))) ...).....1919 / 1.17 (MAPCAR (FUNCTION (LAMBDA (X) (1+ X)...).....1997 / 1.12 (MAPCAR (QUOTE (QUOTE ((X) (1+ X))))...).....2246 / 1.00 Elapsed milliseconds / relative speed for 16384 iteration(s): (MAPCAR (QUOTE (LAMBDA (X) (1+ X))) ...).....1592 / 1.19 (MAPCAR (FUNCTION (LAMBDA (X) (1+ X)...).....1606 / 1.18 (MAPCAR (QUOTE (QUOTE ((X) (1+ X))))...).....1888 / 1.00
"Of course that (function (lambda ( x ) ... )) was faster"
I ran the benchmark 25 times, quoted lambda was faster than function lambda on every single one of them.
So... really "Of course"?
|
|