List operations¶
Most objects that can be of variable size are represented as lists (linked lists internally). Yacas does implement arrays, which are faster when the number of elements in a collection of objects doesn’t change. Operations on lists have better support in the current system.
-
Head(list)¶ returns the first element of a list
This function returns the first element of a list. If it is applied to a general expression, it returns the first operand. An error is returned if
listis an atom.- Example
In> Head({a,b,c}) Out> a; In> Head(f(a,b,c)); Out> a;
-
Tail(list)¶ returns a list without its first element
- Example
In> Tail({a,b,c}) Out> {b,c};
-
Length(list)¶ -
Length(string) The length of a list or string
- Example
In> Length({a,b,c}) Out> 3; In> Length("abcdef"); Out> 6;
-
Map(fn, list)¶ apply an n-ary function to all entries in a list
This function applies
fnto every list of arguments to be found inlist. So the first entry oflistshould be a list containing the first, second, third, … argument tofn, and the same goes for the other entries oflist. The function can either be given as a string or as a pure function (seeApply()for more information on pure functions).- Example
In> Map("+",{{a,b},{c,d}}); Out> {a+c,b+d};See also
-
MapSingle(fn, list)¶ apply a unary function to all entries in a list
The function
fnis successively applied to all entries inlist, and a list containing the respective results is returned. The function can be given either as a string or as a pure function (seeApply()for more information on pure functions).The
/@operator provides a shorthand forMapSingle().- Example
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};
-
MakeVector(var, n)¶ vector of uniquely numbered variable names
A list of length
nis generated. The first entry contains the identifiervarwith the number 1 appended to it, the second entry containsvarwith the suffix 2, and so on until the last entry which containsvarwith the numbernappended to it.- Example
In> MakeVector(a,3) Out> {a1,a2,a3};See also
-
Select(pred, list)¶ select entries satisfying some predicate
Select()returns a sublist oflistwhich contains all the entries for which the predicatepredreturnsTruewhen applied to this entry.- Example
In> Select("IsInteger",{a,b,2,c,3,d,4,e,f}) Out> {2,3,4};
-
Nth(list, n)¶ return the
n-th element of a listThe entry with index
nfromlistis returned. The first entry has index 1. It is possible to pick several entries of the list by takingnto be a list of indices.More generally,
Nthreturns then-th operand of the expression passed as first argument.An alternative but equivalent form of
Nth(list, n)islist[n].- Example
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;
-
Reverse(list)¶ return the reversed list (without touching the original)
- Param list
list to reverse
This function returns a list reversed, without changing the original list. It is similar to
DestructiveReverse(), but safer and slower.- Example
In> lst:={a,b,c,13,19} Out> {a,b,c,13,19}; In> revlst:=Reverse(lst) Out> {19,13,c,b,a}; In> lst Out> {a,b,c,13,19};See also
-
List(expr1, expr2, ...)¶ construct a list
A list is constructed whose first entry is
expr1, the second entry isexpr2, and so on. This command is equivalent to the expression{expr1, expr2, ...}.- Example
In> List(); Out> {}; In> List(a,b); Out> {a,b}; In> List(a,{1,2},d); Out> {a,{1,2},d};
-
UnList(list)¶ convert a list to a function application
This command converts a list to a function application. The first entry of
listis treated as a function atom, and the following entries are the arguments to this function. So the function referred to in the first element oflistis applied to the other elements.Note that
listis evaluated before the function application is formed, but the resulting expression is left unevaluated. The functionsUnList()andHold()both stop the process of evaluation.- Example
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;
-
Listify(expr)¶ convert a function application to a list
The parameter
expris expected to be a compound object, i.e. not an atom. It is evaluated and then converted to a list. The first entry in the list is the top-level operator in the evaluated expression and the other entries are the arguments to this operator. Finally, the list is returned.- Example
In> Listify(Cos(x)); Out> {Cos,x}; In> Listify(3*a); Out> {*,3,a};
-
Concat(list1, list2, ...)¶ concatenate lists
The lists
list1,list2, … are evaluated and concatenated. The resulting big list is returned.- Example
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)}};See also
-
Delete(list, n)¶ delete an element from a list
This command deletes the
n-th element fromlist. The first parameter should be a list, whilenshould be a positive integer less than or equal to the length oflist. The entry with indexnis removed (the first entry has index 1), and the resulting list is returned.- Example
In> Delete({a,b,c,d,e,f}, 4); Out> {a,b,c,e,f};See also
-
Insert(list, n, expr)¶ insert an element into a list
The expression
expris inserted just before then-th entry inlist. The first parameterlistshould be a list, whilenshould be a positive integer less than or equal to the length oflistplus one. The expressionexpris placed between the entries inlistwith indicesn-1andn. There are two border line cases: ifnis 1, the expressionexpris placed in front of the list (just as by the:()operator); ifnequals the length oflistplus one, the expressionexpris placed at the end of the list (just as byAppend()). In any case, the resulting list is returned.- Example
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};See also
-
Replace(list, n, expr)¶ replace an entry in a list
The
n-th entry oflistis replaced by the expressionexpr. This is equivalent to callingDelete()andInsert()in sequence. To be precise, the expressionReplace(list, n, expr)has the same result as the expressionInsert(Delete(list, n), n, expr).- Example
In> Replace({a,b,c,d,e,f}, 4, x); Out> {a,b,c,x,e,f};See also
-
FlatCopy(list)¶ copy the top level of a list
A copy of
listis made and returned. The list is not recursed into, only the first level is copied. This is useful in combination with the destructive commands that actually modify lists in place (for efficiency).The following shows a possible way to define a command that reverses a list nondestructively.
- Example
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};
-
Contains(list, expr)¶ test whether a list contains a certain element
This command tests whether
listcontains the expressionexpras an entry. It returnsTrueif it does andFalseotherwise. Only the top level oflistis examined. The parameterlistmay also be a general expression, in that case the top-level operands are tested for the occurrence ofexpr.- Example
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;
-
Find(list, expr)¶ get the index at which a certain element occurs
This commands returns the index at which the expression
exproccurs inlist. Ifexproccurs more than once, the lowest index is returned. Ifexprdoes not occur at all, -1 is returned.- Example
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;See also
-
Append(list, expr)¶ append an entry at the end of a list
The expression
expris appended at the end oflistand the resulting list is returned.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.
- Example
In> Append({a,b,c,d}, 1); Out> {a,b,c,d,1};See also
-
RemoveDuplicates(list)¶ remove any duplicates from a list
This command removes all duplicate elements from a given list and returns the resulting list. To be precise, the second occurrence of any entry is deleted, as are the third, the fourth, etc.
- Example
In> RemoveDuplicates({1,2,3,2,1}); Out> {1,2,3}; In> RemoveDuplicates({a,1,b,1,c,1}); Out> {a,1,b,c};
-
Swap(list, i1, i2)¶ swap two elements in a list
This command swaps the pair of entries with entries
i1andi2inlist. So the element at indexi1ends up at indexi2and the entry ati2is put at indexi1. Both indices should be valid to address elements in the list. Then the updated list is returned.Swap()works also on generic arrays.- Example
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};See also
-
Count(list, expr)¶ count the number of occurrences of an expression
This command counts the number of times that the expression
exproccurs inlistand returns this number.- Example
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;See also
-
FillList(expr, n)¶ fill a list with a certain expression
This command creates a list of length
nin which all slots contain the expressionexprand returns this list.- Example
In> FillList(x, 5); Out> {x,x,x,x,x};See also
-
Drop(list, n)¶ -
Drop(list, -n) -
Drop(list, {m, n}) drop a range of elements from a list
This command removes a sublist of
listand returns a list containing the remaining entries. The first calling sequence drops the firstnentries inlist. The second form drops the lastnentries. The last invocation drops the elements with indicesmthroughn.- Example
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};
-
Take(list, n)¶ -
Take(list, -n) -
Take(list, {m, n}) take a sublist from a list, dropping the rest
This command takes a sublist of
list, drops the rest, and returns the selected sublist. The first calling sequence selects the firstnentries inlist. The second form takes the lastnentries. The last invocation selects the sublist beginning with entry numbermand ending with then-th entry.- Example
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};
-
Partition(list, n)¶ partition a list in sublists of equal length
This command partitions
listinto non-overlapping sublists of lengthnand returns a list of these sublists. The firstnentries inlistform the first partition, the entries from positionn+1up to2nform the second partition, and so on. Ifndoes not divide the length oflist, the remaining entries will be thrown away. Ifnequals zero, an empty list is returned.- Example
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}};See also
-
Flatten(expression, operator)¶ flatten expression w.r.t. some operator
Flatten()flattens an expression with respect to a specific operator, converting the result into a list. This is useful for unnesting an expression.Flatten()is typically used in simple simplification schemes.- Example
In> Flatten(a+b*c+d, "+"); Out> {a,b*c,d}; In> Flatten({a,{b,c},d}, "List"); Out> {a,b,c,d};See also
-
UnFlatten(list, operator, identity)¶ inverse operation of
Flatten()UnFlatten()is the inverse operation ofFlatten(). Given a list, it can be turned into an expression representing for instance the addition of these elements by callingUnFlatten()with+as argument to operator, and 0 as argument to identity (0 is the identity for addition, since a+0=a). For multiplication the identity element would be 1.- Example
In> UnFlatten({a,b,c},"+",0) Out> a+b+c; In> UnFlatten({a,b,c},"*",1) Out> a*b*c;See also
-
Type(expr)¶ return the type of an expression
The type of the expression
expris represented as a string and returned. So, ifexpris a list, the string"List"is returned. In general, the top-level operator ofexpris returned. If the argumentexpris an atom, the result is the empty string"".- Example
In> Type({a,b,c}); Out> "List"; In> Type(a*(b+c)); Out> "*"; In> Type(123); Out> "";
-
NrArgs(expr)¶ return number of top-level arguments
This function evaluates to the number of top-level arguments of the expression
expr. The argumentexprmay not be an atom, since that would lead to an error.- Example
In> NrArgs(f(a,b,c)) Out> 3; In> NrArgs(Sin(x)); Out> 1; In> NrArgs(a*(b+c)); Out> 2;
-
VarList(expr)¶ -
VarListArith(expr)¶ -
VarListSome(expr, list)¶ list of variables appearing in an expression
The command
VarList()returns a list of all variables that appear in the expressionexpr. The expression is traversed recursively.The command
VarListSome()looks only at arguments of functions in thelist. All other functions are considered opaque (as if they do not contain any variables) and their arguments are not checked. For example,VarListSome(a + Sin(b-c))will return{a, b, c}, butVarListSome(a*Sin(b-c), {*})will not look at arguments ofSin()and will return{a,Sin(b-c)}. HereSin(b-c)is considered a variable because the functionSin()does not belong tolist.The command “func:VarListArith returns a list of all variables that appear arithmetically in the expression
expr. This is implemented throughVarListSome()by restricting to the arithmetic functions+,-,*,/. Arguments of other functions are not checked.Note that since the operators
+and-are prefix as well as infix operators, it is currently required to useAtom("+")to obtain the unevaluated atom+.- Example
In> VarList(Sin(x)) Out> {x}; In> VarList(x+a*y) Out> {x,a,y}; In> VarListSome(x+a*y, {Atom("+")}) Out> {x,a*y}; In> VarListArith(x+y*Cos(Ln(x)/x)) Out> {x,y,Cos(Ln(x)/x)} In> VarListArith(x+a*y^2-1) Out> {x,a,y^2};See also
IsFreeOf(),IsVariable(),FuncList(),HasExpr(),HasFunc()
-
FuncList(expr)¶ list of functions used in an expression
The command
FuncList()returns a list of all function atoms that appear in the expressionexpr. The expression is recursively traversed.- Example
In> FuncList(x+y*Cos(Ln(x)/x)) Out> {+,*,Cos,/,Ln};
-
FuncListArith(expr)¶ list of functions used in an expression
FuncListArith()is defined throughFuncListSome()to look only at arithmetic operations+,-,*,/.- Example
In> FuncListArith(x+y*Cos(Ln(x)/x)) Out> {+,*,Cos};
-
FuncListSome(expr, list)¶ list of functions used in an expression
The command
FuncListSome()does the same asFuncList(), except it only looks at arguments of a givenlistof functions. All other functions become opaque (as if they do not contain any other functions). For example,FuncList(a + Sin(b-c))will see that the expression has a{-}operation and return {{+,Sin,-}}, butFuncListSome(a + Sin(b-c), {+})will not look at arguments ofSin()and will return{+,Sin}.Note that since the operators
+and-are prefix as well as infix operators, it is currently required to useAtom("+")to obtain the unevaluated atom+.- Example
In> FuncListSome({a+b*2,c/d},{List}) Out> {List,+,/};
-
PrintList(list[, padding])¶ print list with padding
Prints
listand inserts thepaddingstring between each pair of items of the list. Items of the list which are strings are printed without quotes, unlikeWrite(). Items of the list which are themselves lists are printed inside braces{}. If padding is not specified, standard one is used “, ” (comma, space).- Example
In> PrintList({a,b,{c, d}}, `` .. ``) Out> `` a .. b .. { c .. d}``;See also
-
Table(body, var, from, to, step)¶ evaluate while some variable ranges over interval
This command generates a list of values from
body, by assigning variablevarvalues fromfromup toto, incrementingstepeach time. So, the variablevarfirst gets the valuefrom, and the expressionbodyis evaluated. Then the valuefrom``+``stepis assigned tovarand the expressionbodyis again evaluated. This continues, incrementingvarwithstepon every iteration, untilvarexceedsto. At that moment, all the results are assembled in a list and this list is returned.- Example
In> Table(i!, i, 1, 9, 1); Out> {1,2,6,24,120,720,5040,40320,362880}; 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};See also
For(),MapSingle(), ..:,TableForm()
-
TableForm(list)¶ print each entry in a list on a line
This functions writes out the list
listin a better readable form, by printing every element in the list on a separate line.- Example
In> TableForm(Table(i!, i, 1, 10, 1)); 1 2 6 24 120 720 5040 40320 362880 3628800 Out> True;
See also
Destructive operations¶
Destructive commands run faster than their nondestructive counterparts because the latter copy the list before they alter it.
-
DestructiveAppend(list, expr)¶ destructively append an entry to a list
This is the destructive counterpart of
Append(). This command yields the same result as the corresponding call toAppend(), but the original list is modified. So if a variable is bound tolist, it will now be bound to the list with the expressionexprinserted.- Example
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};
-
DestructiveDelete(list, n)¶ delete an element destructively from a list
This is the destructive counterpart of :func`Delete`. This command yields the same result as the corresponding call to
Delete(), but the original list is modified. So if a variable is bound tolist, it will now be bound to the list with then-th entry removed.- Example
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};See also
-
DestructiveInsert(list, n, expr)¶ insert an element destructively into a list
This is the destructive counterpart of
Insert(). This command yields the same result as the corresponding call toInsert(), but the original list is modified. So if a variable is bound tolist, it will now be bound to the list with the expressionexprinserted.- Example
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};See also
-
DestructiveReplace(list, n, expr)¶ replace an entry destructively in a list
- Param list
list of which an entry should be replaced
- Param n
index of entry to replace
- Param expr
expression to replace the
n-th entry with
This is the destructive counterpart of
Replace(). This command yields the same result as the corresponding call toReplace(), but the original list is modified. So if a variable is bound tolist, it will now be bound to the list with the expressionexprinserted.- Example
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};See also
-
DestructiveReverse(list)¶ reverse a list destructively
This command reverses
listin place, so that the original is destroyed. This means that any variable bound tolistwill now have an undefined content, and should not be used any more. The reversed list is returned.- Example
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};See also
Set operations¶
-
Intersection(l1, l2)¶ return the intersection of two lists
The intersection of the lists
l1andl2is determined and returned. The intersection contains all elements that occur in both lists. The entries in the result are listed in the same order as inl1. If an expression occurs multiple times in bothl1andl2, then it will occur the same number of times in the result.- Example
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};See also
-
Union(l1, l2)¶ return the union of two lists
The union of the lists
l1andl2is determined and returned. The union contains all elements that occur in one or both of the lists. In the resulting list, any element will occur only once.- Example
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};See also
-
Difference(l1, l2)¶ return the difference of two lists
The difference of the lists
l1andl2is determined and returned. The difference contains all elements that occur inl1but not inl2. The order of elements inl1is preserved. If a certain expression occursn1times in the first list andn2times in the second list, it will occurn1-n2times in the result ifn1is greater thann2and not at all otherwise.- Example
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};See also
Associative map¶
-
Assoc(key, alist)¶ return element stored in association list
The association list
alistis searched for an entry stored with indexkey. If such an entry is found, it is returned. Otherwise the atomEmptyis returned.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 asalist[key].- Example
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;See also
AssocIndices(),[](),:=(),AssocDelete()
-
AssocIndices(alist)¶ return the keys in an association list
All the keys in the association list
alistare assembled in a list and this list is returned.- Example
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``};See also
-
AssocDelete(alist, key)¶ -
AssocDelete(alist, {key, value}) delete an entry in an association list
The key {
key} in the association listalistis deleted. (The list itself is modified.) If the key was found and successfully deleted, returnsTrue, otherwise if the given key was not found, the function returnsFalse.The second, longer form of the function deletes the entry that has both the specified key and the specified value. It can be used for two purposes:
to make sure that we are deleting the right value;
if several values are stored on the same key, to delete the specified entry (see the last example).
At most one entry is deleted.
- Example
In> writer := {}; Out> {}; In> writer[``Iliad``] := ``Homer``; Out> True; In> writer[``Henry IV``] := ``Shakespeare``; Out> True; In> writer[``Ulysses``] := ``James Joyce``; Out> True; In> AssocDelete(writer, ``Henry IV``) Out> True; In> AssocDelete(writer, ``Henry XII``) Out> False; In> writer Out> {{``Ulysses``,``James Joyce``}, {``Iliad``,``Homer``}}; In> DestructiveAppend(writer, {``Ulysses``, ``Dublin``}); Out> {{``Iliad``,``Homer``},{``Ulysses``,``James Joyce``}, {``Ulysses``,``Dublin``}}; In> writer[``Ulysses``]; Out> ``James Joyce``; In> AssocDelete(writer,{``Ulysses``,``James Joyce``}); Out> True; In> writer Out> {{``Iliad``,``Homer``},{``Ulysses``,``Dublin``}};See also
Sorting¶
-
BubbleSort(list, compare)¶ sort a list
This command returns
listafter it is sorted usingcompareto compare elements. The functioncompareshould accept two arguments, which will be elements oflist, and compare them. It should returnTrueif in the sorted list the second argument should come after the first one, andFalseotherwise.The function
BubbleSort()uses the so-called bubble sort algorithm to do the sorting by swapping elements that are out of order. This algorithm is easy to implement, though it is not particularly fast. The sorting time is proportional to \(n^2\) where \(n\) is the length of the list.- Example
In> BubbleSort({4,7,23,53,-2,1}, "<"); Out> {-2,1,4,7,23,53};See also
-
HeapSort(list, compare)¶ sort a list
This command returns
listafter it is sorted usingcompareto compare elements. The functioncompareshould accept two arguments, which will be elements oflist, and compare them. It should returnTrueif in the sorted list the second argument should come after the first one, andFalseotherwise.The function
HeapSort()uses theheapsort algorithmand is much faster for large lists. The sorting time is proportional to \(n\ln(n)\) where \(n\) is the length of the list.- Example
In> HeapSort({4,7,23,53,-2,1}, ``>``); Out> {53,23,7,4,1,-2};See also
Stack and queue operations¶
-
Push(stack, expr)¶ add an element on top of a stack
This is part of a simple implementation of a stack, internally represented as a list. This command pushes the expression
expron top of the stack, and returns the stack afterwards.- Example
In> stack := {}; Out> {}; In> Push(stack, x); Out> {x}; In> Push(stack, x2); Out> {x2,x}; In> PopFront(stack); Out> x2;See also
-
Pop(stack, n)¶ remove an element from a stack
This is part of a simple implementation of a stack, internally represented as a list. This command removes the element with index
nfrom the stack and returns this element. The top of the stack is represented by the index 1. Invalid indices, for example indices greater than the number of element on the stack, lead to an error.- Example
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};See also
-
PopFront(stack)¶ remove an element from the top of a stack
This is part of a simple implementation of a stack, internally represented as a list. This command removes the element on the top of the stack and returns it. This is the last element that is pushed onto the stack.
- Example
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};
-
PopBack(stack)¶ remove an element from the bottom of a stack
This is part of a simple implementation of a stack, internally represented as a list. This command removes the element at the bottom of the stack and returns this element. Of course, the stack should not be empty.
- Example
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};See also
Global stack¶
The functions below operate on a global stack, currently implemented as a list
that is not accessible externally (it is protected through
LocalSymbols()).
-
GlobalPop()¶ -
GlobalPop(var) restore variables using a global stack
GlobalPop()removes the last pushed value from the stack. If a variable name is given, the variable is assigned, otherwise the popped value is returned. If the global stack is empty, an error message is printed.See also
-
GlobalPush(expr)¶ save variables using a global stack
- Example
In> GlobalPush(3) Out> 3; In> GlobalPush(Sin(x)) Out> Sin(x); In> GlobalPop(x) Out> Sin(x); In> GlobalPop(x) Out> 3; In> x Out> 3;
See also