Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

(wx)Maxima: how to iterate an action over every member of a list?

I'm wondering if there is a functional way to apply an action to every element of list, in Maxima, without necessarily looping over the list?

e.g. if I would like to remove every element of the list a:[1,2,3] from the list b:[5,4,3,2,1]. Obviously, something like:

f(a,b):=
block(
[aList:a, newList:b],
for k thru length(aList)
do newList: delete(aList[k],newList) 
);

I just wondered if there was a more direct way? I thought apply might work, but couldn't figure it out, as it seems to take the whole list as the argument (vs. list elements).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a few different ways to accomplish that. One way is to treat the arguments as sets and apply setdifference.

(%i2) a: [1, 2, 3] $              

(%i3) b: [5, 4, 3, 2, 1] $

(%i4) setify(a);
(%o4)                       {1, 2, 3}
(%i5) setify(b);
(%o5)                    {1, 2, 3, 4, 5}
(%i6) setdifference (setify(b), setify(a));
(%o6)                        {4, 5}
(%i7) listify(%);
(%o7)                        [4, 5]

That works if a and b are really sets, i.e. order doesn't matter, and elements are unique.

Another way:

(%i8) sublist (b, lambda ([x], not member(x, a)));
(%o8)                        [5, 4]

I guess the sublist approach makes fewer assumptions, so it is more generally applicable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...