Next Page Contents

listlib::insert -- insert an element into a list

Introduction

listlib::insert(list, element) inserts element into list.

Call(s)

listlib::insert(list, element <, function>)

Parameters

list - MuPAD list
element - MuPAD expression to insert
function - function, that determines the insert position

Returns

the given list enlarged with the inserted element

Related Functions

_concat, append, listlib::insertAt

Details

Example 1

Insert 3 into the given ordered list:

>> listlib::insert([1, 2, 4, 5, 6], 3)
                            [1, 2, 3, 4, 5, 6]

Insert 3 into the given descending ordered list. The insert function represents and preserves the order of the list:

>> listlib::insert([6, 5, 4, 2, 1], 3, _not@_less)
                            [6, 5, 4, 3, 2, 1]

Because identifiers cannot be ordered by _less, another function must be given, e.g., the function that represents the systems internal order:

>> listlib::insert([a, b, d, e, f], c, sysorder)
                            [a, b, c, d, e, f]

Example 2

Because no function is given as third argument, the function _less is used. _less is called: _less(1, 3), _less(2, 3), _less(4, 3) and then 3 is inserted in front of 4:

>> listlib::insert([1, 2, 4], 3)
                               [1, 2, 3, 4]

If the list is not ordered right, then the insert position could be wrong:

>> listlib::insert([4, 1, 2], 3)
                               [3, 4, 1, 2]

Example 3

The following example shows, how expressions can be ordered by a user defined priority. This order is given by the function named priority, which returns a smaller number, when the expression has a type with higher priority:

>> priority := X -> contains(["_power", "_mult", "_plus"], type(X)):
   priority(x^2), priority(x + 2)
                                   1, 3

The function sortfunc returns TRUE, if the both given arguments are in the right order, i.e., the first argument has a higher (or equal) priority than the second argument:

>> sortfunc := (X, Y) -> bool(priority(Y) > priority(X)):
   sortfunc(x^2, x + 2), sortfunc(x + 2, x*2)
                                TRUE, FALSE

Now the expression x*2 is inserted at the ``right'' place in the list:

>> listlib::insert([x^y, x^2, x*y, -y, x + y], x*2, sortfunc)
                         y   2
                       [x , x , 2 x, x y, -y, x + y]

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000