0

我无法在 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));

输出 ::[[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

4

2 回答 2

2

通过 concat 构造表达式并通过 parse_string 评估它们几乎肯定不是解决问题的好方法。我的建议是不要费心去弄清楚为什么 takewhile1 不起作用,这不值得麻烦。

关于这个问题的内置函数,sublist返回满足谓词的元素并sublist_indices返回它们在列表中的位置,同时join将两个列表粘贴在一起。所以也许你可以写:

take_while (x, p) := join (sublist (x, p), sublist_indices (x, p));
于 2014-03-20T17:05:52.560 回答
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]]
于 2014-03-20T19:13:15.360 回答