compareLists
lib.compareLists
Docs pulled from | This Revision | 10 minutes ago
Compare two lists element-by-element with a comparison function cmp
.
List elements are compared pairwise in order by the provided comparison function cmp
,
the first non-equal pair of elements determines the result.
The <
operator can also be used to compare lists using a boolean condition. (e.g. [1 2] < [1 3]
is true
).
See also language operators for more information.
Inputs
cmp
-
The comparison function
a: b: ...
must return:0
ifa
andb
are equal1
ifa
is greater thanb
-1
ifa
is less thanb
See lib.compare for a an example implementation.
a
-
The first list
b
-
The second list
Examples
lib.lists.compareLists
usage examples
compareLists lib.compare [] []
=> 0
compareLists lib.compare [] [ "a" ]
=> -1
compareLists lib.compare [ "a" ] []
=> 1
compareLists lib.compare [ "a" "b" ] [ "a" "c" ]
=> -1
Noogle detected
Implementation
The following is the current implementation of this function.
compareLists =
cmp: a: b:
if a == [ ] then
if b == [ ] then 0 else -1
else if b == [ ] then
1
else
let
rel = cmp (head a) (head b);
in
if rel == 0 then compareLists cmp (tail a) (tail b) else rel;