6.3.8 Removing elements from a list
Elements can be removed from a list by using either the
suppress
command (which removes a single element) or the
remove command
(which removes all elements meeting a given conditions).
-
suppress takes two arguments:
-
L, a list.
- i, a nonnegative integer.
- suppress(L,i) returns the list L
with the element at index i removed.
- remove takes two arguments:
-
f, a boolean function.
- L, a list.
- remove(f,L) returns the sublist of L with the
elements c such that f(c)==true removed.
Examples
remove(x->(x>=2),[0,1,2,3,1,5]) |
You can use remove to remove
characters from a string. For example, to remove all instances of
a given letter from a string, enter the following code in a program
editor level (see Section 24.1.2 for writing functions):
f(s,c):={
local ordc,l,r,x,k;
ordc:=ord(c);
l:=length(s)-1;
purge(x,k);
r:=remove(x->(ord(x)==ordc),seq(s[k],k,0,l));
return char(ord(r));
} |
Here s is the input string an c is the letter
to be removed. Now, for example: