我找不到任何关于Maxima的文档。所以,我尝试编写自己的文件,给出了元素和它们的位置。一旦满足条件,它就开始从列表中的任何位置开始。我最终得到了两个具有细微差别的函数,
takewhile(x,p):=block([s:1,temp:[],temp1:[],count:1,xx:create_list([x[i],i],i,makelist(i,i,length(x)))],
for i in xx do if apply(p,[first(i)]) then temp:cons(i,temp) ,temp:reverse(temp),
if(length(temp)>=2 and flatten(temp)#[]) then
(while(count<length(temp) and last(temp[s])+1=last(temp[s+1]) ) do
(temp1:cons(temp[s],temp1),count:count+1,s:s+1),
if(s<=length(temp)) then (temp1:cons(temp[s],temp1)) else print("Exceeded")) else temp1:temp,reverse(temp1))$
用法::
takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>3));
OUTPUT ::
[[4,5],[5,6],[7,7],[4,8]]
和第二,
takewhile1(x,p):=block([s:1,temp:[],temp1:[],count:1,xx:create_list([x[i],i],i,makelist(i,i,length(x)))],
for i in xx do (if parse_string(concat(first(i),p)) then temp:cons(i,temp)) ,temp:reverse(temp),
if(length(temp)>=2 and flatten(temp)#[]) then
(while(last(temp[s])+1=last(temp[s+1]) and count<length(temp)) do
(temp1:cons(temp[s],temp1),count:count+1,s:s+1),
if(s<length(temp)) then temp1:cons(temp[s],temp1)) else temp1:temp,reverse(temp1))$
用法::
takewhile1([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],\<5);
OUTPUT ::
[[2,1],[1,2],[2,3],[3,4],[4,5]]
微妙的区别在于使用parse_string
创建lambda函数而不是将lambda作为函数的参数应用。
Problem
:我能做到,
takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x^2+3*x>6));
OUTPUT ::
[[2,1]]
但如果我在使用takewhile1
时返回,我无法获得如何实现它,
concat: argument must be an atom; found ^2>5
答案 0 :(得分:2)
通过concat构造表达式并通过parse_string对它们进行评估几乎肯定不是解决问题的好方法。我的建议是不要试图找出为什么takewhile1不起作用,它不值得麻烦。
关于此问题的内置函数,sublist
返回满足谓词的元素,sublist_indices
返回列表中的位置,而join
将两个列表粘贴在一起。所以也许你可以写:
take_while (x, p) := join (sublist (x, p), sublist_indices (x, p));
答案 1 :(得分:1)
我认为命令式版本更具可读性
load("basic");
takewhile(lst, pr):= block([l: [], c: []],
for el in reverse(lst) do if pr(el) then push(el, c)
else (push(c, l), c: []),
push(c, l),
delete([], l));
试验:
(%i3) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>10));
(%o3) []
(%i4) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>0));
(%o4) [[2, 1, 2, 3, 4, 5, 7, 4, 1, 4, 5, 2, 1, 7, 8]]
(%i5) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>3));
(%o5) [[4, 5, 7, 4], [4, 5], [7, 8]]
(%i6) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x^2+3*x>6));
(%o6) [[2], [2, 3, 4, 5, 7, 4], [4, 5, 2], [7, 8]]
更新:我误解了您对takewhile
的定义。这是我的另一次尝试
takewhile(lst, pr):= block([c: [], n: length(lst)], local(pr),
reverse(catch(for idx thru n do block([el: part(lst, idx)],
if not pr(el) and not emptyp(c) then throw(c)
else if pr(el) then push([el, idx], c)),
c)));
试验:
(%i25) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>10));
(%o25) []
(%i26) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>0));
(%o26) [[2, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [7, 7], [4, 8],
[1, 9], [4, 10], [5, 11], [2, 12], [1, 13], [7, 14], [8, 15]]
(%i27) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x>3));
(%o27) [[4, 5], [5, 6], [7, 7], [4, 8]]
(%i28) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x^2+3*x>6));
(%o28) [[2, 1]]
(%i29) takewhile([2,1,2,3,4,5,7,4,1,4,5,2,1,7,8],lambda([x],x<5));
(%o29) [[2, 1], [1, 2], [2, 3], [3, 4], [4, 5]]