5.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]) |
Remark.
Use remove to remove characters from
a string. For example, to remove all the "a"s of a string
(see Section 23.1.2 for writing functions):
Then (in a program editor level):
f(chn):={
local l:=length(chn)-1;
return remove(x->(ord(x)==orda),seq(chn[k],k,0,l));
} |
Now:
|
| ⎡
⎣ | “b”,“r”,“c”,“d”,“b”,“r” | ⎤
⎦ |
| | | | | | | | | | |
|
To get a string:
char(ord(["b","r","c","d","b","r"])) |