len package:base

Returns the size/length of a finite structure as an Int. The default implementation just counts elements starting with the leftmost. Instances for structures that can compute the element count faster than via element-by-element counting, should provide a specialised implementation.

Examples

Basic usage:
>>> length []
0
>>> length ['a', 'b', 'c']
3

>>> length [1..]
* Hangs forever *
Number of elements in NonEmpty list.
Return the number of elements in an array, excluding the terminator
length returns the length of a finite list as an Int. It is an instance of the more general genericLength, the result type of which may be any kind of number.
>>> length []
0

>>> length ['a', 'b', 'c']
3

>>> length [1..]
* Hangs forever *
Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10. While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.
>>> compareLength [] 0
EQ

>>> compareLength [] 1
LT

>>> compareLength ['a'] 1
EQ

>>> compareLength ['a', 'b'] 1
GT

>>> compareLength [0..] 100
GT

>>> compareLength undefined (-1)
GT

>>> compareLength ('a' : undefined) 0
GT
The genericLength function is an overloaded version of length. In particular, instead of returning an Int, it returns any type which is an instance of Num. It is, however, less efficient than length.

Examples

>>> genericLength [1, 2, 3] :: Int
3

>>> genericLength [1, 2, 3] :: Float
3.0
Users should take care to pick a return type that is wide enough to contain the full length of the list. If the width is insufficient, the overflow behaviour will depend on the (+) implementation in the selected Num instance. The following example overflows because the actual list length of 200 lies outside of the Int8 range of -128..127.
>>> genericLength [1..200] :: Int8
-56
Returns the size/length of a finite structure as an Int.

Examples

Basic usage:
>>> bilength (True, 42)
2
>>> bilength (Right 42)
1
>>> bilength (BiList [1,2,3] [4,5])
5
>>> bilength (BiList [] [])
0
On infinite structures, this function hangs:
> bilength (BiList [1..] [])
* Hangs forever *
This data type represents an equivalence relation. Equivalence relations are expected to satisfy three laws: The types alone do not enforce these laws, so you'll have to check them yourself.
Check for equivalence with ==. Note: The instances for Double and Float violate reflexivity for NaN.
Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10. While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.
>>> compareLength ('a' :| []) 1
EQ

>>> compareLength ('a' :| ['b']) 3
LT

>>> compareLength (0 :| [1..]) 100
GT

>>> compareLength undefined 0
GT

>>> compareLength ('a' :| 'b' : undefined) 1
GT
A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).
A wide character string with explicit length information in CWchars instead of a terminating NUL (allowing NUL characters in the middle of the string).
Marshal a Haskell string into a C string (ie, character array) with explicit length information.
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a C string (ie, character array) with explicit length information.
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a C wide string (ie, wide character array) with explicit length information.
  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.
Marshal a C string with explicit length into a Haskell string.
Marshal a C string with explicit length into a Haskell string.
Marshal a C wide string with explicit length into a Haskell string.
Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
Marshal a Haskell string into a C wide string (i.e. wide character array) in temporary storage, with explicit length information.
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
Like withArray, but the action gets the number of values as an additional parameter