listlib::singleMerge --
merging of two ordered lists without duplicates
Introductionlistlib::singleMerge(list1, list2) merges
two ordered lists without duplicates.
Call(s)listlib::singleMerge(list1, list2 <,
function>)
Parameterslist1, list2 |
- | a MuPAD list |
function |
- | a function, that determines the merging order |
Returnsan ordered list that contains the elements of both lists
Related
Functionslistlib::merge,
listlib::insert,
_concat, zip
Detailslistlib::singleMerge(list1, list2) merges
the both lists into one list. It is assumed that the lists are
``disjunct'', no element appears in both lists. Otherwise such elements
are inserted only once in the result list.TRUE, if the two elements are in the right
order, otherwise FALSE (see next paragraph).FALSE. Then the element of the second
list is inserted into the first list in front of the last
proved element (see example 3).The lists must be ordered with regard to the order function, otherwise the elements could be inserted at the wrong place.
_less is used. If no order of the
elements with regard to _less is defined, a function must be
given, otherwise an error appears. The system function sysorder always can be used.
Example
1Merging two ascending ordered lists:
>> listlib::singleMerge([1, 3, 5, 7], [2, 4, 6, 8])
[1, 2, 3, 4, 5, 6, 7, 8]
Merging two descending ordered lists:
>> listlib::singleMerge([7, 5, 3, 1], [8, 6, 4, 2], _not@_less)
[8, 7, 6, 5, 4, 3, 2, 1]
Example
2Merging two ascending ordered lists with duplicates:
>> listlib::singleMerge([1, 2, 5, 7], [2, 5, 6, 8])
[1, 2, 5, 6, 7, 8]
But the following lists does not contain mutual equal elements:
>> listlib::singleMerge([1, 1, 3, 3], [2, 2, 4, 4])
[1, 1, 2, 2, 3, 3, 4, 4]
Example
3The 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 both lists are merged with regard to the given priority:
>> listlib::singleMerge([x^y, x*2, -y], [x^2, x*y, x + y], sortfunc)
y 2
[x , x , 2 x, -y, x y, x + y]
>> delete priority, sortfunc:
listtools::singleMerge