| Head | The first element of a list |
| Tail | Returns a list without its first element |
| Length | The length of a list or string |
| Map | Apply an n-ary function to all entries in a list |
| MapSingle | Apply a unary function to all entries in a list |
| RandomIntegerVector | Generate a vector of random integers |
| MakeVector | Vector of uniquely numbered variable names |
| Select | Select the entries satisfying some predicate |
| Nth | Return the n-th element of a list |
| DestructiveReverse | Reverse a list destructively |
| List | Construct a list |
| UnList | Convert a list to a function application |
| Listify | Convert a function application to a list |
| Concat | Concatenate lists |
| Delete | Delete an element from a list |
| Insert | Insert an element into a list |
| DestructiveDelete | Delete an element destructively from a list |
| DestructiveInsert | Insert an element destructively into a list |
| Replace | Replace an entry in a list |
| DestructiveReplace | Replace an entry in a list destructively |
| FlatCopy | Copy the top level of a list |
| Contains | Test whether a list contains a certain element |
| Find | The index at which a certain element occurs |
| Append | Append an entry at the end of a list |
| DestructiveAppend | Destructively append an entry to a list |
| RemoveDuplicates | Remove any duplicates from a list |
| Push | Add an element on top of a stack |
| Pop | Remove an element from a stack |
| PopFront | Remove an element from the top of a stack |
| PopBack | Remove an element from the bottom of a stack |
| Swap | Swap two elements in a list |
| Count | Count the number of occurrences of an expression |
| Intersection | Return the intersection of two lists |
| Union | Return the union of two lists |
| Difference | Return the difference of two lists |
| FillList | Fill a list with a certain expression |
| Drop | Drop a range of elements from a list |
| Take | Take a sublist from a list, dropping the rest |
| Partition | Partition a list in sublists of equal length |
| Assoc | Return element stored in association list |
| AssocIndices | Return the keys in an association list |
| Flatten | Flatten expression w.r.t. some operator |
| UnFlatten | Inverse operation of Flatten |
| Type | Return the type of an expression |
| NrArgs | Return number of top-level arguments |
| BubbleSort | Sort a list |
| Table | Evaluate while some variable ranges over interval |
| TableForm | Print each entry in a list on a line |
In> Head({a,b,c})
Out> a;
In> Head(f(a,b,c));
Out> a;
|
In> Tail({a,b,c})
Out> {b,c};
|
In> Length({a,b,c})
Out> 3;
In> Length("abcdef");
Out> 6;
|
In> MapSingle("Sin",{a,b,c});
Out> {Sin(a),Sin(b),Sin(c)};
In> Map("+",{{a,b},{c,d}});
Out> {a+c,b+d}; |
The /@ operator provides a shorthand for MapSingle.
In> MapSingle("Sin",{a,b,c});
Out> {Sin(a),Sin(b),Sin(c)};
In> MapSingle({{x},x^2}, {a,2,c});
Out> {a^2,4,c^2}; |
In> RandomIntegerVector(4,-3,3)
Out> {0,3,2,-2};
|
In> MakeVector(a,3)
Out> {a1,a2,a3};
|
In> Select("IsInteger",{a,b,2,c,3,d,4,e,f})
Out> {2,3,4};
|
More generally, Nth returns the n-th operand of the expression passed as first argument.
An alternative but equivalent form of Nth(list, n) is list[n].
In> lst := {a,b,c,13,19};
Out> {a,b,c,13,19};
In> Nth(lst, 3);
Out> c;
In> lst[3];
Out> c;
In> Nth(lst, {3,4,1});
Out> {c,13,a};
In> Nth(b*(a+c), 2);
Out> a+c; |
Destructive commands are faster than their nondestructive counterparts. Stangely, there is no nondestructive command to reverse a list. Use FlatCopy and DestructiveReverse to achieve this.
In> lst := {a,b,c,13,19};
Out> {a,b,c,13,19};
In> revlst := DestructiveReverse(lst);
Out> {19,13,c,b,a};
In> lst;
Out> {a}; |
In> List();
Out> {};
In> List(a,b);
Out> {a,b};
In> List(a,{1,2},d);
Out> {a,{1,2},d}; |
Note that "list" is evaluated before the function application is formed, but the resulting expression is not evaluated.
In> UnList({Cos, x});
Out> Cos(x);
In> UnList({f});
Out> f();
In> UnList({Taylor,x,0,5,Cos(x)});
Out> Taylor(x,0,5)Cos(x);
In> Eval(%);
Out> 1-x^2/2+x^4/24; |
In> Listify(Cos(x));
Out> {Cos,x};
In> Listify(3*a);
Out> {*,3,a}; |
In> Concat({a,b}, {c,d});
Out> {a,b,c,d};
In> Concat({5}, {a,b,c}, {{f(x)}});
Out> {5,a,b,c,{f(x)}}; |
In> Delete({a,b,c,d,e,f}, 4);
Out> {a,b,c,e,f}; |
In> Insert({a,b,c,d}, 4, x);
Out> {a,b,c,x,d};
In> Insert({a,b,c,d}, 5, x);
Out> {a,b,c,d,x};
In> Insert({a,b,c,d}, 1, x);
Out> {x,a,b,c,d}; |
Destructive commands run faster than their nondestructive counterparts because the latter copy the list before they alter it.
In> lst := {a,b,c,d,e,f};
Out> {a,b,c,d,e,f};
In> Delete(lst, 4);
Out> {a,b,c,e,f};
In> lst;
Out> {a,b,c,d,e,f};
In> DestructiveDelete(lst, 4);
Out> {a,b,c,e,f};
In> lst;
Out> {a,b,c,e,f}; |
Destructive commands run faster than their nondestructive counterparts because the latter copy the list before they alter it.
In> lst := {a,b,c,d};
Out> {a,b,c,d};
In> Insert(lst, 2, x);
Out> {a,x,b,c,d};
In> lst;
Out> {a,b,c,d};
In> DestructiveInsert(lst, 2, x);
Out> {a,x,b,c,d};
In> lst;
Out> {a,x,b,c,d}; |
In> Replace({a,b,c,d,e,f}, 4, x);
Out> {a,b,c,x,e,f}; |
Destructive commands run faster than their nondestructive counterparts because the latter copy the list before they alter it.
In> lst := {a,b,c,d,e,f};
Out> {a,b,c,d,e,f};
In> Replace(lst, 4, x);
Out> {a,b,c,x,e,f};
In> lst;
Out> {a,b,c,d,e,f};
In> DestructiveReplace(lst, 4, x);
Out> {a,b,c,x,e,f};
In> lst;
Out> {a,b,c,x,e,f}; |
In> reverse(l_IsList) <-- DestructiveReverse(FlatCopy(l));
Out> True;
In> lst := {a,b,c,d,e};
Out> {a,b,c,d,e};
In> reverse(lst);
Out> {e,d,c,b,a};
In> lst;
Out> {a,b,c,d,e}; |
In> Contains({a,b,c,d}, b);
Out> True;
In> Contains({a,b,c,d}, x);
Out> False;
In> Contains({a,{1,2,3},z}, 1);
Out> False;
In> Contains(a*b, b);
Out> True; |
In> Find({a,b,c,d,e,f}, d);
Out> 4;
In> Find({1,2,3,2,1}, 2);
Out> 2;
In> Find({1,2,3,2,1}, 4);
Out> -1; |
Note that due to the underlying data structure, the time it takes to append an entry at the end of a list grows linearly with the length of the list, while the time for prepending an entry at the beginning is constant.
In> Append({a,b,c,d}, 1);
Out> {a,b,c,d,1}; |
Destructive commands run faster than their nondestructive counterparts because the latter copy the list before they alter it.
In> lst := {a,b,c,d};
Out> {a,b,c,d};
In> Append(lst, 1);
Out> {a,b,c,d,1};
In> lst
Out> {a,b,c,d};
In> DestructiveAppend(lst, 1);
Out> {a,b,c,d,1};
In> lst;
Out> {a,b,c,d,1}; |
In> RemoveDuplicates({1,2,3,2,1});
Out> {1,2,3};
In> RemoveDuplicates({a,1,b,1,c,1});
Out> {a,1,b,c}; |
In> stack := {};
Out> {};
In> Push(stack, x);
Out> {x};
In> Push(stack, x2);
Out> {x2,x};
In> PopFront(stack);
Out> x2; |
In> stack := {};
Out> {};
In> Push(stack, x);
Out> {x};
In> Push(stack, x2);
Out> {x2,x};
In> Push(stack, x3);
Out> {x3,x2,x};
In> Pop(stack, 2);
Out> x2;
In> stack;
Out> {x3,x}; |
In> stack := {};
Out> {};
In> Push(stack, x);
Out> {x};
In> Push(stack, x2);
Out> {x2,x};
In> Push(stack, x3);
Out> {x3,x2,x};
In> PopFront(stack);
Out> x3;
In> stack;
Out> {x2,x}; |
In> stack := {};
Out> {};
In> Push(stack, x);
Out> {x};
In> Push(stack, x2);
Out> {x2,x};
In> Push(stack, x3);
Out> {x3,x2,x};
In> PopBack(stack);
Out> x;
In> stack;
Out> {x3,x2}; |
In> lst := {a,b,c,d,e,f};
Out> {a,b,c,d,e,f};
In> Swap(lst, 2, 4);
Out> {a,d,c,b,e,f}; |
In> lst := {a,b,c,b,a};
Out> {a,b,c,b,a};
In> Count(lst, a);
Out> 2;
In> Count(lst, c);
Out> 1;
In> Count(lst, x);
Out> 0; |
In> Intersection({a,b,c}, {b,c,d});
Out> {b,c};
In> Intersection({a,e,i,o,u}, {f,o,u,r,t,e,e,n});
Out> {e,o,u};
In> Intersection({1,2,2,3,3,3}, {1,1,2,2,3,3});
Out> {1,2,2,3,3}; |
In> Union({a,b,c}, {b,c,d});
Out> {a,b,c,d};
In> Union({a,e,i,o,u}, {f,o,u,r,t,e,e,n});
Out> {a,e,i,o,u,f,r,t,n};
In> Union({1,2,2,3,3,3}, {2,2,3,3,4,4});
Out> {1,2,3,4}; |
In> Difference({a,b,c}, {b,c,d});
Out> {a};
In> Difference({a,e,i,o,u}, {f,o,u,r,t,e,e,n});
Out> {a,i};
In> Difference({1,2,2,3,3,3}, {2,2,3,4,4});
Out> {1,3,3}; |
In> FillList(x, 5);
Out> {x,x,x,x,x}; |
In> lst := {a,b,c,d,e,f,g};
Out> {a,b,c,d,e,f,g};
In> Drop(lst, 2);
Out> {c,d,e,f,g};
In> Drop(lst, -3);
Out> {a,b,c,d};
In> Drop(lst, {2,4});
Out> {a,e,f,g}; |
In> lst := {a,b,c,d,e,f,g};
Out> {a,b,c,d,e,f,g};
In> Take(lst, 2);
Out> {a,b};
In> Take(lst, -3);
Out> {e,f,g};
In> Take(lst, {2,4});
Out> {b,c,d}; |
In> Partition({a,b,c,d,e,f,}, 2);
Out> {{a,b},{c,d},{e,f}};
In> Partition(1 .. 11, 3);
Out> {{1,2,3},{4,5,6},{7,8,9}}; |
Association lists are represented as a list of two-entry lists. The first element in the two-entry list is the key, the second element is the value stored under this key.
The call Assoc(key, alist) can (probably more intuitively) be accessed as alist[key].
In> writer := {};
Out> {};
In> writer["Iliad"] := "Homer";
Out> True;
In> writer["Henry IV"] := "Shakespeare";
Out> True;
In> writer["Ulysses"] := "James Joyce";
Out> True;
In> Assoc("Henry IV", writer);
Out> {"Henry IV","Shakespeare"};
In> Assoc("War and Peace", writer);
Out> Empty; |
In> writer := {};
Out> {};
In> writer["Iliad"] := "Homer";
Out> True;
In> writer["Henry IV"] := "Shakespeare";
Out> True;
In> writer["Ulysses"] := "James Joyce";
Out> True;
In> AssocIndices(writer);
Out> {"Iliad","Henry IV","Ulysses"}; |
In> Flatten(a+b*c+d,"+");
Out> {a,b*c,d};
In> Flatten({a,{b,c},d},"List");
Out> {a,b,c,d};
|
In> UnFlatten({a,b,c},"+",0)
Out> a+b+c;
In> UnFlatten({a,b,c},"*",1)
Out> a*b*c;
|
In> Type({a,b,c});
Out> "List";
In> Type(a*(b+c));
Out> "*";
In> Type(123);
Out> ""; |
In> NrArgs(f(a,b,c)) Out> 3; In> NrArgs(Sin(x)); Out> 1; In> NrArgs(a*(b+c)); Out> 2; |
This function uses the so-called BubbleSort algorithm to do the sorting. This algorithm is chosen because it is easy to implement, though it is not particularly fast.
In> BubbleSort({4,7,23,53,-2,1}, "<");
Out> {-2,1,4,7,23,53};
In> BubbleSort({4,7,23,53,-2,1}, ">");
Out> {53,23,7,4,1,-2}; |
In> Table(i!, i, 1, 10, 1);
Out> {1,2,6,24,120,720,5040,40320,362880,3628800};
In> Table(i, i, 3, 16, 4);
Out> {3,7,11,15};
In> Table(i^2, i, 10, 1, -1);
Out> {100,81,64,49,36,25,16,9,4,1}; |
In> TableForm(Table(i!, i, 1, 10, 1)); 1 2 6 24 120 720 5040 40320 362880 3628800 Out> True; |