我只是想编写一些类似于Mathematica
's partition function的函数,并在 maxima 中传递选项,
listpartitionpad(l,n,k,d):= block([temp:[],gap,newl,ntemp:[]],
newl:apply(create_listpad,flatten([n,k,d,l])),
map(lambda([x],if(length(newl)>=x+n-1 and is(unique[x]#[d]))then temp:cons(part(newl,makelist(i,i,x,x+n-1)),temp)
else temp:cons(part(newl,makelist(s,s,x,length(newl))),temp)),makelist(i,i,1,length(newl),k)),
ntemp:sublist(temp,lambda([x],is(length(x)=n))),reverse(ntemp));
Usage
:listpartitionpad([a,b,c,d,e,f,g],3,3,x); => [[a,b,c],[d,e,f],[g,x,x]]
现在,正如我检查的那样,所有列表操作函数都用 lisp 编码。
我的问题是,我可以用 maxima 语言而不是 lisp 编写任何这样的函数,这很好,否则它会给我一些性能问题或其他我应该知道但我还不知道的东西。我做了一个简单的测试
:lisp(time(loop repeat 1000000))
real time : 0.850 secs
run-gbc time : 0.540 secs
child run time : 0.000 secs
gbc time : 0.310 secs
在另一种基于最大值的方法的情况下,
block([],for i:1 thru 1000000 do []);
评估耗时 5.7700 秒(经过 5.7700 秒)
这种差异随着增长呈指数i
增长。
这是所有列表操作都用 lisp 编码的原因吗?