Skip to content

StdOrdList

This module can be imported directly or as a part of the StdEnv module. It provides definitions for list-ordering functions.

Visit StdOrdList on Cloogle for source code of this module.

Basic functions

These functions are not too difficult to define, but they are convenient to use.

sort

Signature

// CLEAN

sort :: [T] -> [T] | Ord T
sort    ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Sorts the list in ascending order. Under the hood, this function uses merge sort.

Usage

// CLEAN

sort [1, 1, 1]  // [1, 1, 1]
sort [1, 2, 3]  // [1, 2, 3]
sort [2, 1, 3]  // [1, 2, 3]
sort []         // []

maxList

Signature

// CLEAN

maxList :: [T] -> T | Ord T
maxList    ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Returns the largest element of the list. Under the hood, this function uses the equality and less than operations to determine which element to return.

Results in a run-time error if the given list is empty.

$ maxList of []

Usage

// CLEAN

maxList [1, 1, 1]  // 1
maxList [1, 2, 3]  // 3
maxList []         // run-time error

minList

Signature

// CLEAN

minList :: [T] -> T | Ord T
minList    ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Returns the smallest element of the list. Under the hood, this function uses the equality and less than operations to determine which element to return.

Results in a run-time error if the given list is empty.

$ minList of []

Usage

// CLEAN

minList [1, 1, 1]  // 1
minList [1, 2, 3]  // 1
minList []         // run-time error

Higher-order functions

sortBy

Signature

// CLEAN

sortBy :: (T -> T -> Bool) [T] -> [T]
sortBy    sortRule         ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Sorts the list based on the given sortRule function.

Usage

// CLEAN

sortBy (>) [1, 1, 1]  // [1, 1, 1]
sortBy (>) [1, 2, 3]  // [3, 2, 1]
sortBy (>) [2, 1, 3]  // [3, 2, 1]
sortby (>) []         // []

maxListBy

Signature

// CLEAN

maxListBy :: (T -> T -> Bool) [T] -> [T]
maxListBy    maxRule          ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Returns the largest element from the given list. The largest element is determined by the given maxRule function.

Results in a run-time error if the given list is empty.

$ maxListBy of []

Usage

// CLEAN

maxListBy (<) [1, 2, 3]  // 3
maxListBy (>) [1, 1, 1]  // 1
maxListBy (>) [1, 2, 3]  // 1
maxListBy (>) []         // run-time error

minListBy

Signature

// CLEAN

maxListBy :: (T -> T -> Bool) [T] -> [T]
maxListBy    minRule          ls  => ...

The type T must be an instance of the Ord class from the StdClass module.

Behavior

Returns the smallest element from the given list. The smallest element is determined by the given minRule function.

Results in a run-time error if the given list is empty.

$ minListBy of []

Usage

// CLEAN

minListBy (<) [1, 2, 3]  // 1
minListBy (>) [1, 1, 1]  // 1
minListBy (>) [1, 2, 3]  // 3
minListBy (>) []         // run-time error