我在Maxima.So中找不到任何文件,我试着编写自己的文档,给出两个元素,只要条件满足,它们的position.It就会从列表中的任何地方开始。最后我得到了两个有细微差别的函数,
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));输出::[[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);输出::[[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));输出::[[2,1]]
但我不知道如果我在返回时使用takewhile1,我将如何实现它,
concat: argument must be an atom; found ^2>5
发布于 2014-03-20 19:13:15
我认为命令版本的可读性会大大提高。
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]]更新:I误解了您对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]]发布于 2014-03-20 17:05:52
通过concat构造表达式并通过parse_string对它们进行评估几乎肯定不是解决问题的好方法。我的建议是,不要费心找出为什么takewhile1不工作,这不值得的麻烦。
关于这个问题的内置函数,sublist返回满足谓词的元素,sublist_indices返回它们在列表中的位置,而join则将两个列表粘贴在一起。所以也许你可以写:
take_while (x, p) := join (sublist (x, p), sublist_indices (x, p));https://stackoverflow.com/questions/22526472
复制相似问题