Methods
(static) insert(e, xs) → {List}
The insert
function takes an element and a List
and inserts the element into the list at the
first position where it is less than or equal to the next element. In particular, if the list is
sorted before the call, the result will also be sorted. Use insertBy
to supply your own
comparison function.
Haskell> insert :: Ord a => a -> [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
e |
* | The element to insert |
xs |
List | The |
- Source:
Returns:
A new List
, with the element inserted
- Type
- List
Example
const lst = list(1,2,3,4,5,6,8,9,10);
insert(7, lst); // => [1:2:3:4:5:6:7:8:9:10:[]]
(static) insertBy(cmp, e, as) → {List}
Insert an element into a list using a comparison function of your choice.
Haskell> insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
cmp |
function | The comparison function—must return an |
e |
* | The element to insert |
as |
List | The |
- Source:
Returns:
A new List
, with the element inserted
- Type
- List
Example
const notCompare = (x, y) => compare(x, y) === EQ ? EQ : (GT ? LT : GT);
const lst = list(1,2,3,4,5,6,8,9,10);
insertBy(notCompare, 7, lst); // => [7:1:2:3:4:5:6:8:9:10:[]]
(static) mergeSort(xs) → {List}
Sort a list using regular value comparison. Use mergeSortBy
to supply your own comparison
function. Uses a merge sort algorithm, which may be more efficient than sort
for larger lists.
Haskell> sort :: Ord a => [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
xs |
List | The |
- Source:
Returns:
- The sorted
List
(the original list is unmodified)
- Type
- List
Example
const lst1 = list(20,19,18,17,16,15,14,13,12,11,10,1,2,3,4,5,6,7,8,9);
mergeSort(lst1); // => [1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:[]]
const f = x => x + 1;
const lst2 = reverse(listRange(1, 11, f)); // [10:9:8:7:6:5:4:3:2:1:[]]
mergeSort(lst2); // => [1:2:3:4:5:6:7:8:9:10:[]]
(static) mergeSortBy(cmp, as) → {List}
Sort a list using a comparison function of your choice. Uses a merge sort algorithm, which may be
more efficient than sortBy
for larger lists.
Haskell> sortBy :: (a -> a -> Ordering) -> [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
cmp |
function | The comparison function—must return an |
as |
List | The |
- Source:
Returns:
The sorted List
(the original list is unmodified)
- Type
- List
Example
const notCompare = (x, y) => compare(x, y) === EQ ? EQ : (GT ? LT : GT);
const lst1 = listRange(1, 11);
const lst2 = reverse(lst1); // [10:9:8:7:6:5:4:3:2:1:[]]
mergeSortBy(notCompare, lst1); // => [1:2:3:4:5:6:7:8:9:10:[]]
mergeSortBy(notCompare, lst2); // => [10:9:8:7:6:5:4:3:2:1:[]]
(static) sort(xs) → {List}
Sort a list using regular value comparison. Use sortBy
to supply your own comparison function.
Uses an insertion sort algorithm. The mergeSort
function is probably more efficient for larger
lists.
Haskell> sort :: Ord a => [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
xs |
List | The |
- Source:
Returns:
The sorted List
(the original list is unmodified)
- Type
- List
Example
const lst = list(9,8,7,6,5,4,3,10,13,11,14,23,24,26,25,2,1);
sort(lst); // => [1:2:3:4:5:6:7:8:9:10:11:13:14:23:24:25:26:[]]
(static) sortBy(cmp, xs) → {List}
Sort a list using a comparison function of your choice. Uses an insertion sort algorithm. The
mergeSortBy
function is probably more efficient for larger lists.
Haskell> sortBy :: (a -> a -> Ordering) -> [a] -> [a]
Parameters:
Name | Type | Description |
---|---|---|
cmp |
function | The comparison function—must return an |
xs |
List | The |
- Source:
Returns:
The sorted List
(the original list is unmodified)
- Type
- List
Example
const notCompare = (x, y) => compare(x, y) === EQ ? EQ : (GT ? LT : GT);
const lst1 = listRange(1, 11);
const lst2 = reverse(lst1); // [10:9:8:7:6:5:4:3:2:1:[]]
sortBy(notCompare, lst1); // => [1:2:3:4:5:6:7:8:9:10:[]]
sortBy(notCompare, lst2); // => [10:9:8:7:6:5:4:3:2:1:[]]
(static) zip(as, bs) → {List}
Take two List
objects and return a List
of corresponding pairs. If one input list is shorter,
excess elements of the longer list are discarded.
Haskell> zip :: [a] -> [b] -> [(a, b)]
Parameters:
Name | Type | Description |
---|---|---|
as |
List | The first |
bs |
List | The second |
- Source:
Returns:
The zipped List
of Tuple
objects
- Type
- List
Example
const lst1 = list(1,2,3,4,5);
const lst2 = list(5,4,3,2,1);
zip(lst1, lst2); // => [(1,5):(2,4):(3,3):(4,2):(5,1):[]]
(static) zip3(as, bs, cs) → {List}
Take three List
objects and return a List
of triples (Tuple
objects with three values).
Analogous to the zip
function.
Haskell> zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
Parameters:
Name | Type | Description |
---|---|---|
as |
List | The first |
bs |
List | The second |
cs |
List | The third |
- Source:
Returns:
The zipped List
of Tuple
objects
- Type
- List
Example
const lst1 = list(1,2,3,4,5);
const lst2 = list(5,4,3,2,1);
const lst3 = list(6,7,8,9,10);
zip3(lst1, lst2, lst3); // => [(1,5,6):(2,4,7):(3,3,8):(4,2,9):(5,1,10):[]]
(static) zipWith(f, as, bs) → {List}
A generalization of the zip
function. Zip two List
objects using a provided function.
Haskell> zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The zipping function |
as |
List | The first |
bs |
List | The second |
- Source:
Returns:
The zipped List
- Type
- List
Example
const lst1 = list(1,2,3,4,5);
const lst2 = list(5,4,3,2,1);
const f = (x, y) => tuple(x * 3, y ** 2);
const g = (x, y) => x + y;
zipWith(f, lst1, lst2); // => [(3,25):(6,16):(9,9):(12,4):(15,1):[]]
zipWith(g, lst1, lst2); // => [6:6:6:6:6:[]]
(static) zipWith3(f, as, bs, cs) → {List}
A generalization of the zip3
function. Zip three List
objects using a provided function.
Haskell> zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The zipping function |
as |
List | The first |
bs |
List | The second |
cs |
List | The third |
- Source:
Returns:
The zipped List
- Type
- List
Example
const lst1 = list(1,2,3,4,5);
const lst2 = list(5,4,3,2,1);
const lst3 = list(6,7,8,9,10);
const f = (x, y, z) => tuple(x * 3, y * y, z % 2);
const g = (x, y, z) => x + y + z;
zipWith3(f, lst1, lst2, lst3); // => [(3,25,0):(6,16,1):(9,9,0):(12,4,1):(15,1,0):[]]
zipWith3(g, lst1, lst2, lst3); // => [12:13:14:15:16:[]]