乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Jef!

[编程交流] 更容易递归地构建

[复制链接]

0

主题

375

帖子

385

银币

限制会员

铜币
-7
发表于 2022-7-5 13:52:18 | 显示全部楼层
很公平。出于调试原因,我不检查这种可能的错误。当然,一旦例程没有bug,我就不会添加它
141957tlt6ky6i9el39e0l.png
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 13:56:51 | 显示全部楼层
FWIW检查初始输入(由Subfo的用户提供)的一种折衷方法是将递归转换为尾部递归:
然后您可以通过使用一些输入进行比较:
因此,当你打算用mapcar映射该函数时,你需要为这两个函数都添加撇号
然而,你不能这样做:
由于编译到vlx需要使用函数而不是撇号,这意味着您将无法将此语法转换为。vlx文件
总之,这种类型的函数是匿名的,并且具有列表类型:
IMO由于其被视为未评估列表,我发现将其放入()的assoc列表中是可行的。
所以你可能会发现这很有趣。
回复

使用道具 举报

28

主题

317

帖子

292

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
140
发表于 2022-7-5 14:00:25 | 显示全部楼层
也许是好奇心+灵感的产物?
1.mapcar函数帮助中的好奇心,
(mapcar’1+(列表a b c));为什么不解释“1+?
2.Inspiration=LM:acdoc/defun-q;Lee quotes list作为函数
在mapcar中尝试了Lee的“list函数”,但失败了,然后尝试使用额外的引号进行调试,然后成功了。
 
感谢Grrr提及
同意,双引号没有什么特别的,它仍然可以与三引号,但也许更多的报价更有趣?
  1. 19
具有相似性,但vlx优化除外。VLIDE显示生成输出消息:
;警告:错误参数:(引号((…)( ... )))
141958qb5xbdn3nf34xmt1.png
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 14:01:45 | 显示全部楼层
据我所知,这些结果是最好的性能,因为代码片段适用于您编写它的特定任务。。。
我做了一些测试,比较了(函数(lambda(x)…)和“”((x)…)。。。当然(函数(λ(x)…))速度更快。。。似乎在您投资于编码的时候-更发达的代码-(函数(lambda(…)…)结果在速度优化中得到了回报。。。当然,我有更大的代码,我做了完全相同的例子DWG参考实体测试。。。我的测试表明(函数(lambda(…)…)是5,6,7,有时甚至比“”(……)快X(倍)。当然,有一个代码是为编译准备的,所以它在每个函数上都有(例如,“,(function-)的语法),另一个是为了测试目的,我将每个(function car)替换为“car example…”。。。
我还没有用(mapcar)(lambda(…)…)做过测试由于这不是完全编码(函数)函数,也不是完全引用。。。
HTH。,M、 R。
回复

使用道具 举报

28

主题

317

帖子

292

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
140
发表于 2022-7-5 14:06:59 | 显示全部楼层
大家早上好!
嗯,对我来说,知道和理解之间是有差距的。有了你和吉尔的例子,我现在(有点)知道了。我还是不太明白。阅读Hanhphuc的“尝试在mapcar中使用Lee的‘list函数’,但失败了,然后尝试使用额外的引号进行调试,然后成功了。”我想他和我在同一条船上;他通过试错到达那里。我理解结构,比如(mapcar’lambda或(mapcar(function)(lambda)。我也有点理解为什么(mapcar(function’((x)…)。。。不起作用,并返回“坏函数参数”,因为您对其类型的演示是列表格式。但是,在mapcar的帮助下,第一个参数应该是函数类型Subr,在所有逻辑中,它不应该像'((x)…)那样以相同的错误爆炸。。。是一个列表而不是subr?嗯。让我们玩得开心,制造更多的困惑^^假设我(用列表1、2、3,而不是列表a、b、c)。。。像你一样,为什么需要引用1+:我知道,但我真的不明白。原因如下
(mapcar“1+(列表1 2 3)),(mapcar(函数1+(列表1 2 3)),(mapcar“1+”(1 2 3))等。。工作和回报都一样。Mapcar需要一个列表作为第二个参数,(键入“(1-2-3))实际上返回一个列表。到现在为止,一直都还不错。。。令人难以置信的事情:
Mapcar需要一个函数/subr作为第一个参数
(类型(函数1+)和(类型“1+)都返回SYM。
(类型1+)返回SUBR
(mapcar 1+(列表1 2 3))将出错并返回;错误:功能错误:#
基本上,虽然mapcar的参数#2是一个列表,(type’(1 2 3))实际上返回了一个列表,但其第一个参数应该是subr,但(type‘1+)返回SYM。但我知道mapcar的工作原理。 
我从未使用过vlx,但如果我的理解是正确的,两者都可以编译成vlx,区别在于引用的lambda可以以某种方式查看。打开一个新的绘图时,我忘记了vlide中的“动画”选项,出现了一堆“源”窗口,每个窗口包含一个lambda,而整个acaddoc/startup/etc都是动画。 
 
间接是的:正因为如此,正如hanh所指出的,它是唯一一个不使用递归的。这也是为什么我说这是一个不平衡的苹果香蕉比较
虽然我不确定我是否完全同意“函数(lambda)”是一种更先进的代码,或者编写速度优化的代码更耗时(一旦你知道,你知道,这不像添加“函数”这个词需要那么多时间),但我同意始终记在心里的速度是一种很好的做法。我不明白你说的“这不是完全编码的(函数)函数,也不是完全引用的……”是什么意思。当涉及到lambda和/或vlx时,可能影响更大,但如果我对(mapcar’1+(列表1 2 3))和(mapcar(函数1+(列表1 2 3))进行基准测试,我不会得到看起来非常显著的速度差
我真的会努力找到更好/更快的方法来实现一些需要用户感知的时间量的事情,即使它只是一个“口吃”,比如0.1秒或更多。
(mapcar(功能1+)(列表1 2 3))是(mapcar’1+(列表1 2 3))的更发达/更好版本?这个例子需要运行10000次才能保存10个ms。我在这里扮演devils advocate,因为想当然地认为该函数比引用lambdas更好更快,我有一些惊喜。
“当然(函数(λ(x)…)更快”
我运行了25次基准测试,引用的lambda比上的函数lambda都快。
所以真的“当然”吗
142000kssvnz3040o3byxx.png
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 14:08:24 | 显示全部楼层
只有我的2美分。。。
  1. 22

 
  1. 23

 
  1. 24
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 14:12:43 | 显示全部楼层
142001op0s00h1s3grnd2o.png
回复

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-5 14:16:08 | 显示全部楼层
As far as I can tell, those results yeld best performance as the snippet was applicable to your specific task for which it was written...
        I did some testings with comparison of (function (lambda ( x ) ...)) and ''(( x ) ...) ... Of course that (function (lambda ( x ) ... )) was faster... It seems that for the time you invest in coding - more developed code - (function (lambda ( ... ) ... )) the results pay off in speed optimizations... Of course I had bigger code and I did tests on exactly the same example DWG reference entity... My testings showed that (function (lambda ( ... ) ... )) were 5,6,7 and sometimes even more X (times) faster than ''(( ... ) ... ). Of course one code was prepared for compiling - so it had on every function (for ex. '- syntax of (function -)) and the other one was for testing purposes where I replaced every (function car) with 'car for example...
        I haven't done testings with (mapcar '(lambda ( ... ) ... )) as this is somewhat neither fully coded (function) function, neither fully quoted...
        HTH., M.R.
回复

使用道具 举报

28

主题

317

帖子

292

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
140
发表于 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. 142002gop45yob34mkbtyy.png
        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
  1. 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.
  1. 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"? 142003ivvkzohodkzghgdz.png
回复

使用道具 举报

76

主题

312

帖子

254

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
390
发表于 2022-7-5 14:22:42 | 显示全部楼层
Just my 2 cents...
       
  1. (setq Lst      '((-4 (0 (269.157 85.9418 0.0)) (0 (272.343 85.9418 0.0)) (0 (272.343 59.9463 0.0)) (0 (269.157 59.9463 0.0)))       (-3 (0 (273.021 84.9263 0.0)) (0 (274.851 84.9263 0.0)) (0 (274.851 83.3016 0.0)) (0 (273.021 83.3016 0.0)))       (-2 (0 (275.258 65.4297 0.0)) (0 (279.528 65.4297 0.0)) (0 (279.528 60.6233 0.0)) (0 (275.258 60.6233 0.0)))       (-1 (0 (280.206 72.2671 0.0)) (0 (283.596 72.2671 0.0)) (0 (283.596 69.5592 0.0)) (0 (280.206 69.5592 0.0)))      ))
         
       
  1. (foreach x Lst    (foreach y x        (if (listp y)            (setq testlist (cons (cadr y) testlist))        )    ))
         
       
  1. ((280.206 69.5592 0.0) (283.596 69.5592 0.0) (283.596 72.2671 0.0) (280.206 72.2671 0.0) (275.258 60.6233 0.0) (279.528 60.6233 0.0) (279.528 65.4297 0.0) (275.258 65.4297 0.0) (273.021 83.3016 0.0) (274.851 83.3016 0.0) (274.851 84.9263 0.0) (273.021 84.9263 0.0) (269.157 59.9463 0.0) (272.343 59.9463 0.0) (272.343 85.9418 0.0) (269.157 85.9418 0.0))
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-15 07:26 , Processed in 0.366539 second(s), 73 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表