### inits examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `inits`, which returns all initial segments of a list, shortest first. ```haskell >>> **inits "abc" **["","a","ab","abc"] ``` ```haskell >>> **inits [] **[[]] ``` -------------------------------- ### inits1 examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `inits1`, which returns all non-empty initial segments as `NonEmpty` lists. ```haskell >>> **inits1 "abc" **['a' :| "",'a' :| "b",'a' :| "bc"] ``` ```haskell >>> **inits1 [] **[] ``` -------------------------------- ### iterate examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of iterate with different functions. The last example shows that iterate id is equivalent to repeat. ```haskell >>> **take 10 $ iterate not True **[True,False,True,False,True,False,True,False,True,False] ``` ```haskell >>> **take 10 $ iterate (+3) 42 **[42,45,48,51,54,57,60,63,66,69] ``` ```haskell >>> **take 10 $ iterate id 1 **[1,1,1,1,1,1,1,1,1,1] ``` -------------------------------- ### subsequences basic example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of subsequences returning all subsequences of a string. ```haskell >>> subsequences "abc" ["","a","b","ab","c","ac","bc","abc"] ``` -------------------------------- ### product examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for product, which computes the product of numbers in a structure. It hangs on infinite lists. ```haskell >>> **product [] **1 ``` ```haskell >>> **product [42] **42 ``` ```haskell >>> **product [1..10] **3628800 ``` ```haskell >>> **product [4.1, 2.0, 1.7] **13.939999999999998 ``` ```haskell >>> **product [1..] *** Hangs forever * ``` -------------------------------- ### concat examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for concat, which concatenates all elements of a container of lists. ```haskell >>> **concat (Just [1, 2, 3]) **[1,2,3] ``` ```haskell >>> **concat (Left 42) **[] ``` ```haskell >>> **concat [[1, 2, 3], [4, 5], [6], []] **[1,2,3,4,5,6] ``` -------------------------------- ### unzip3 examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the unzip3 function, which splits a list of triples into three lists. ```haskell >>> **unzip3 [] **([],[],[]) ``` ```haskell >>> **unzip3 [(1, 'a', True), (2, 'b', False)] **([1,2],"ab",[True,False]) ``` -------------------------------- ### unzip examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the unzip function from Data.List, showing the result for an empty list and a list of pairs. ```haskell >>> **unzip [] **([],[]) ``` ```haskell >>> **unzip [(1, 'a'), (2, 'b')] **([1,2],"ab") ``` -------------------------------- ### take examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of take with various inputs, including negative and zero counts, showing the prefix behavior. ```haskell >>> **take 5 "Hello World!" **"Hello" ``` ```haskell >>> **take 3 [1,2,3,4,5] **[1,2,3] ``` ```haskell >>> **take 3 [1,2] **[1,2] ``` ```haskell >>> **take 3 [] **[] ``` ```haskell >>> **take (-1) [1,2] **[] ``` ```haskell >>> **take 0 [1,2] **[] ``` -------------------------------- ### repeat examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of repeat: creates an infinite list with the same value. The second example shows that forcing repeat undefined raises an exception. ```haskell >>> **take 10 $ repeat 17 **[17,17,17,17,17,17,17,17,17, 17] ``` ```haskell >>> **repeat undefined **[*** Exception: Prelude.undefined ``` -------------------------------- ### cycle examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of cycle: ties a finite list into a circular one. Empty list raises an exception; the last example shows laziness. ```haskell >>> **cycle [] ***** Exception: Prelude.cycle: empty list ``` ```haskell >>> **take 10 (cycle [42]) **[42,42,42,42,42,42,42,42,42,42] ``` ```haskell >>> **take 10 (cycle [2, 5, 7]) **[2,5,7,2,5,7,2,5,7,2] ``` ```haskell >>> **take 1 (cycle (42 : undefined)) **[42] ``` -------------------------------- ### elemIndices examples: all matching indices Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `elemIndices` returning all indices of the query element in ascending order. ```haskell >>> **elemIndices 'o' "Hello World" **[4,7] ``` ```haskell >>> **elemIndices 1 [1, 2, 3, 1, 2, 3] **[0,3] ``` -------------------------------- ### init examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the init function: returning all elements except the last, including a singleton list and an empty list that raises an exception. ```haskell >>> **init [1, 2, 3] **[1,2] ``` ```haskell >>> **init [1] **[] ``` ```haskell >>> **init [] ***** Exception: Prelude.init: empty list ``` -------------------------------- ### tails examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `tails`, which returns all final segments of a list, longest first. ```haskell >>> **tails "abc" **["abc","bc","c",""] ``` ```haskell >>> **tails [1, 2, 3] **[[1,2,3],[2,3],[3],[]] ``` ```haskell >>> **tails [] **[[]] ``` -------------------------------- ### insert examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of insert, which inserts an element into a sorted list at the first position where it is less than or equal to the next element. ```haskell >>> **insert (-1) [1, 2, 3] **[-1,1,2,3] ``` ```haskell >>> **insert 'd' "abcefg" **"abcdefg" ``` ```haskell >>> **insert 4 [1, 2, 3, 5, 6, 7] **[1,2,3,4,5,6,7] ``` -------------------------------- ### all examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for all, which determines whether all elements satisfy the predicate. It can return False on an infinite list if a counterexample is found, but hangs if all elements satisfy it. ```haskell >>> **all (> 3) [] **True ``` ```haskell >>> **all (> 3) [1,2] **False ``` ```haskell >>> **all (> 3) [1,2,3,4,5] **False ``` ```haskell >>> **all (> 3) [1..] **False ``` ```haskell >>> **all (> 3) [4..] *** Hangs forever * ``` -------------------------------- ### singleton examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the singleton function: constructing a list from a single element, including a list and a character. ```haskell >>> **singleton True **[True] ``` ```haskell >>> **singleton [1, 2, 3] **[[1,2,3]] ``` ```haskell >>> **singleton 'c' **"c" ``` -------------------------------- ### scanr1 examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of scanr1 with different operators and edge cases. The last example shows that forcing an infinite list with scanr1 causes a stack overflow. ```haskell >>> **scanr1 (+) [1..4] **[10,9,7,4] ``` ```haskell >>> **scanr1 (+) [] **[] ``` ```haskell >>> **scanr1 (-) [1..4] **[-2,3,-1,4] ``` ```haskell >>> **scanr1 (&&) [True, False, True, True] **[False,False,True,True] ``` ```haskell >>> **scanr1 (||) [True, True, False, False] **[True,True,False,False] ``` ```haskell >>> **force $ scanr1 (+) [1..] ***** Exception: stack overflow ``` -------------------------------- ### splitAt examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of splitAt with various indices, including negative and zero, showing the tuple of prefix and suffix. ```haskell >>> **splitAt 6 "Hello World!" **("Hello ","World!") ``` ```haskell >>> **splitAt 3 [1,2,3,4,5] **([1,2,3],[4,5]) ``` ```haskell >>> **splitAt 1 [1,2,3] **([1],[2,3]) ``` ```haskell >>> **splitAt 3 [1,2,3] **([1,2,3],[]) ``` ```haskell >>> **splitAt 4 [1,2,3] **([1,2,3],[]) ``` ```haskell >>> **splitAt 0 [1,2,3] **([],[1,2,3]) ``` ```haskell >>> **splitAt (-1) [1,2,3] **([],[1,2,3]) ``` -------------------------------- ### replicate examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of replicate: creates a list of length n with the same value. Negative n yields an empty list. ```haskell >>> **replicate 0 True **[] ``` ```haskell >>> **replicate (-1) True **[] ``` ```haskell >>> **replicate 4 True **[True,True,True,True] ``` -------------------------------- ### partition examples: vowels, even, and less-than Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `partition` with different predicates: vowel membership, even numbers, and less-than comparison. Each returns a pair of lists. ```haskell >>> **partition (`elem` "aeiou") "Hello World!" **("eoo","Hll Wrld!") ``` ```haskell >>> **partition even [1..10] **([2,4,6,8,10],[1,3,5,7,9]) ``` ```haskell >>> **partition (< 5) [1..10] **([1,2,3,4],[5,6,7,8,9,10]) ``` -------------------------------- ### map usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of map with different functions: adding 1, identity, and a lambda expression. The identity example illustrates that map id == id. ```haskell >>> map (+1) [1, 2, 3] [2,3,4] ``` ```haskell >>> map id [1, 2, 3] [1,2,3] ``` ```haskell >>> map (\n -> 3 * n + 1) [1, 2, 3] [4,7,10] ``` -------------------------------- ### reverse usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of reverse on finite and infinite lists. The last example shows that reverse on an infinite list hangs forever because the list must be finite. ```haskell >>> reverse [] [] ``` ```haskell >>> reverse [42] [42] ``` ```haskell >>> reverse [2,5,7] [7,5,2] ``` ```haskell >>> reverse [1..] * Hangs forever * ``` -------------------------------- ### group examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `group`, which groups consecutive equal elements into sublists. The result is a list of lists. ```haskell >>> **group "Mississippi" **["M","i","ss","i","ss","i","pp","i"] ``` ```haskell >>> **group [1, 1, 1, 2, 2, 3, 4, 5, 5] **[[1,1,1],[2,2],[3],[4],[5,5]] ``` -------------------------------- ### any examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for any, which determines whether any element satisfies the predicate. It can return True on an infinite list if a match is found, but hangs if no match exists. ```haskell >>> **any (> 3) [] **False ``` ```haskell >>> **any (> 3) [1,2] **False ``` ```haskell >>> **any (> 3) [1,2,3,4,5] **True ``` ```haskell >>> **any (> 3) [1..] **True ``` ```haskell >>> **any (> 3) [0, -1..] *** Hangs forever * ``` -------------------------------- ### transpose basic example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Basic example of transpose transposing rows and columns of a rectangular list. ```haskell >>> transpose [[1,2,3],[4,5,6]] [[1,4],[2,5],[3,6]] ``` -------------------------------- ### scanl usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of scanl with various functions and inputs, including an infinite list and a case where undefined is not forced. ```haskell >>> scanl (+) 0 [1..4] [0,1,3,6,10] ``` ```haskell >>> scanl (+) 42 [] [42] ``` ```haskell >>> scanl (-) 100 [1..4] [100,99,97,94,90] ``` ```haskell >>> scanl (\reversedString nextChar -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd'] ["foo","afoo","bafoo","cbafoo","dcbafoo"] ``` ```haskell >>> take 10 (scanl (+) 0 [1..]) [0,1,3,6,10,15,21,28,36,45] ``` ```haskell >>> take 1 (scanl undefined 'a' undefined) "a" ``` -------------------------------- ### drop examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of drop with various inputs, including negative and zero counts, showing the suffix behavior. ```haskell >>> **drop 6 "Hello World!" **"World!" ``` ```haskell >>> **drop 3 [1,2,3,4,5] **[4,5] ``` ```haskell >>> **drop 3 [1,2] **[] ``` ```haskell >>> **drop 3 [] **[] ``` ```haskell >>> **drop (-1) [1,2] **[1,2] ``` ```haskell >>> **drop 0 [1,2] **[1,2] ``` -------------------------------- ### sum examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for sum, which computes the sum of numbers in a structure. It hangs on infinite lists. ```haskell >>> **sum [] **0 ``` ```haskell >>> **sum [42] **42 ``` ```haskell >>> **sum [1..10] **55 ``` ```haskell >>> **sum [4.1, 2.0, 1.7] **7.8 ``` ```haskell >>> **sum [1..] *** Hangs forever * ``` -------------------------------- ### lines examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the lines function, showing how it splits strings on newline characters, including edge cases for empty input and trailing newlines. ```haskell >>> **lines "" -- empty input contains no lines **[] ``` ```haskell >>> **lines "\n" -- single empty line **[""] ``` ```haskell >>> **lines "one" -- single unterminated line **["one"] ``` ```haskell >>> **lines "one\n" -- single non-empty line **["one"] ``` ```haskell >>> **lines "one\n\n" -- second line is empty **["one",""] ``` ```haskell >>> **lines "one\ntwo" -- second line is unterminated **["one","two"] ``` ```haskell >>> **lines "one\ntwo\n" -- two non-empty lines **["one","two"] ``` -------------------------------- ### foldl1 logical OR example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of foldl1 with logical OR, yielding True. ```haskell >>> **foldl1 (||) [False, False, True, True] **True ``` -------------------------------- ### foldl1 logical AND example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of foldl1 with logical AND, yielding False. ```haskell >>> **foldl1 (&&) [True, False, True, True] **False ``` -------------------------------- ### concatMap examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for concatMap, which maps a function over a container and concatenates the resulting lists. ```haskell >>> **concatMap (take 3) [[1..], [10..], [100..], [1000..]] **[1,2,3,10,11,12,100,101,102,1000,1001,1002] ``` ```haskell >>> **concatMap (take 3) (Just [1..]) **[1,2,3] ``` -------------------------------- ### unwords examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the unwords function, which joins strings with spaces, including handling of empty strings. ```haskell >>> **unwords ["Lorem", "ipsum", "dolor"] **"Lorem ipsum dolor" ``` ```haskell >>> **unwords ["foo", "bar", "", "baz"] **"foo bar baz" ``` -------------------------------- ### unionBy examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of unionBy, the non-overloaded version of union. Both arguments may be infinite. ```haskell >>> **unionBy (>) [3, 4, 5] [1, 2, 3, 4, 5, 6] **[3,4,5,4,5,6] ``` ```haskell >>> **import Data.Semigroup (Arg(..)) **>>> **unionBy (/=) [Arg () "Saul"] [Arg () "Kim"] **[Arg () "Saul", Arg () "Kim"] ``` -------------------------------- ### words examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the words function, which splits a string on whitespace and trims leading/trailing spaces. ```haskell >>> **words "Lorem ipsum\ndolor" **["Lorem","ipsum","dolor"] ``` ```haskell >>> **words " foo bar " **["foo","bar"] ``` -------------------------------- ### tail examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the tail function: returning the elements after the head, including a singleton list and an empty list that raises an exception. ```haskell >>> **tail [1, 2, 3] **[2,3] ``` ```haskell >>> **tail [1] **[] ``` ```haskell >>> **tail [] ***** Exception: Prelude.tail: empty list ``` -------------------------------- ### stripPrefix examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `stripPrefix`, which drops a prefix from a list and returns `Just` the remainder or `Nothing` if the prefix does not match. ```haskell >>> **stripPrefix "foo" "foobar" **Just "bar" ``` ```haskell >>> **stripPrefix "foo" "foo" **Just "" ``` ```haskell >>> **stripPrefix "foo" "barfoo" **Nothing ``` ```haskell >>> **stripPrefix "foo" "barfoobaz" **Nothing ``` -------------------------------- ### break examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the `break` function, which splits a list at the first element satisfying the predicate. The output is shown for each input. ```haskell >>> **break (> 3) [1,2,3,4,1,2,3,4] **([1,2,3],[4,1,2,3,4]) ``` ```haskell >>> **break (< 9) [1,2,3] **([],[1,2,3]) ``` ```haskell >>> **break (> 9) [1,2,3] **([1,2,3],[]) ``` -------------------------------- ### unsnoc examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the unsnoc function: decomposing a list into its init and last, returning Nothing for an empty list. ```haskell >>> **unsnoc [] **Nothing ``` ```haskell >>> **unsnoc [1] **Just ([],1) ``` ```haskell >>> **unsnoc [1, 2, 3] **Just ([1,2],3) ``` -------------------------------- ### takeWhile examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of takeWhile with different predicates, showing the longest prefix satisfying the predicate. ```haskell >>> **takeWhile (< 3) [1,2,3,4,1,2,3,4] **[1,2] ``` ```haskell >>> **takeWhile (< 9) [1,2,3] **[1,2,3] ``` ```haskell >>> **takeWhile (< 0) [1,2,3] **[] ``` -------------------------------- ### lookup examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of lookup, which looks up a key in an association list. Returns Nothing if the key is not found; the list must be finite for a Nothing result. ```haskell >>> **lookup 2 [] **Nothing ``` ```haskell >>> **lookup 2 [(1, "first")] **Nothing ``` ```haskell >>> **lookup 2 [(1, "first"), (2, "second"), (3, "third")] **Just "second" ``` -------------------------------- ### unsnoc laziness examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Laziness examples for unsnoc: showing how it behaves with undefined elements and partial lists. ```haskell >>> **fst <$> unsnoc [undefined] **Just [] ``` ```haskell >>> **head . fst <$> unsnoc (1 : undefined) **Just *** Exception: Prelude.undefined ``` ```haskell >>> **head . fst <$> unsnoc (1 : 2 : undefined) **Just 1 ``` -------------------------------- ### null basic examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Basic usage examples for the null function: testing whether a structure is empty. ```haskell >>> **null [] **True ``` ```haskell >>> **null [1] **False ``` -------------------------------- ### scanl1 usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of scanl1 with various functions, including an empty list, infinite list, and a case where undefined is not forced. ```haskell >>> scanl1 (+) [1..4] [1,3,6,10] ``` ```haskell >>> scanl1 (+) [] [] ``` ```haskell >>> scanl1 (-) [1..4] [1,-1,-4,-8] ``` ```haskell >>> scanl1 (&&) [True, False, True, True] [True,False,False,False] ``` ```haskell >>> scanl1 (||) [False, False, True, True] [False,False,True,True] ``` ```haskell >>> take 10 (scanl1 (+) [1..]) [1,3,6,10,15,21,28,36,45,55] ``` ```haskell >>> take 1 (scanl1 undefined ('a' : undefined)) "a" ``` -------------------------------- ### head examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the head function: extracting the first element of a list, including an infinite list and an empty list that raises an exception. ```haskell >>> **head [1, 2, 3] **1 ``` ```haskell >>> **head [1..] **1 ``` ```haskell >>> **head [] ***** Exception: Prelude.head: empty list ``` -------------------------------- ### findIndices examples: all matching indices Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `findIndices` returning all indices of elements satisfying the predicate in ascending order. ```haskell >>> **findIndices (`elem` "aeiou") "Hello World!" **[1,4,7] ``` ```haskell >>> **findIndices (\l -> length l > 3) ["a", "bcde", "fgh", "ijklmnop"] **[1,3] ``` -------------------------------- ### minimumBy example with length comparison Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of `minimumBy` finding the least element by length. The result is `"!"` because it has the smallest length. ```haskell >>> **minimumBy (compare `on` length) ["Hello", "World", "!", "Longest", "bar"] **"!" ``` -------------------------------- ### uncons examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the uncons function: decomposing a list into its head and tail, returning Nothing for an empty list. ```haskell >>> **uncons [] **Nothing ``` ```haskell >>> **uncons [1] **Just (1,[]) ``` ```haskell >>> **uncons [1, 2, 3] **Just (1,[2,3]) ``` -------------------------------- ### permutations usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of permutations for a string, a list of numbers, and the empty list. The order is not lexicographic. ```haskell >>> permutations "abc" ["abc","bac","cba","bca","cab","acb"] ``` ```haskell >>> permutations [1, 2] [[1,2],[2,1]] ``` ```haskell >>> permutations [] [[]] ``` -------------------------------- ### and examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for and, which returns the conjunction of a container of Bools. It returns False if a False occurs finitely far from the left, but hangs on an infinite list of True. ```haskell >>> **and [] **True ``` ```haskell >>> **and [True] **True ``` ```haskell >>> **and [False] **False ``` ```haskell >>> **and [True, True, False] **False ``` ```haskell >>> **and (False : repeat True) -- Infinite list [False,True,True,True,... **False ``` ```haskell >>> **and (repeat True) *** Hangs forever * ``` -------------------------------- ### !? examples: total list indexing Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the total list index operator `!?`. Returns `Just` for valid indices and `Nothing` for out-of-bounds or negative indices. ```haskell >>> **['a', 'b', 'c'] !? 0 **Just 'a' ``` ```haskell >>> **['a', 'b', 'c'] !? 2 **Just 'c' ``` ```haskell >>> **['a', 'b', 'c'] !? 3 **Nothing ``` ```haskell >>> **['a', 'b', 'c'] !? (-1) **Nothing ``` -------------------------------- ### foldl lazy accumulator example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Shows that foldl reverses the input before prepending to the accumulator, so corecursion starts only after traversing the entire input. ```haskell >>> **foldl (\acc c -> c : acc) "abcd" "efgh" **"hgfeabcd" ``` -------------------------------- ### foldr1 examples with various inputs Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for foldr1, a variant of foldr without a base case. It raises a runtime exception on empty structures and hangs on infinite lists. ```haskell >>> **foldr1 (+) [1..4] **10 ``` ```haskell >>> **foldr1 (+) [] **Exception: Prelude.foldr1: empty list ``` ```haskell >>> **foldr1 (+) Nothing ***** Exception: foldr1: empty structure ``` ```haskell >>> **foldr1 (-) [1..4] **-2 ``` ```haskell >>> **foldr1 (&&) [True, False, True, True] **False ``` ```haskell >>> **foldr1 (||) [False, False, True, True] **True ``` ```haskell >>> **foldr1 (+) [1..] *** Hangs forever * ``` -------------------------------- ### tails laziness examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Laziness examples for `tails`, showing that the first element of the result is produced lazily and that the tail can be evaluated without forcing the head. ```haskell >>> **tails undefined **[*** Exception: Prelude.undefined ``` ```haskell >>> **drop 1 (tails [undefined, 1, 2]) **[[1, 2], [2], []] ``` -------------------------------- ### null infinite structure example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example showing that null is expected to terminate even for infinite structures, returning False for an infinite list. ```haskell >>> **null [1..] **False ``` -------------------------------- ### tails1 laziness examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Laziness examples for `tails1`, showing that the first element is produced lazily and that the tail can be evaluated without forcing the head. ```haskell >>> **tails1 undefined ***** Exception: Prelude.undefined ``` ```haskell >>> **drop 1 (tails1 [undefined, 1, 2]) **[1 :| [2],2 :| []] ``` -------------------------------- ### last examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the last function: extracting the last element of a finite list, an infinite list that hangs, and an empty list that raises an exception. ```haskell >>> **last [1, 2, 3] **3 ``` ```haskell >>> **last [1..] *** Hangs forever * ``` ```haskell >>> **last [] ***** Exception: Prelude.last: empty list ``` -------------------------------- ### !! examples: partial list indexing with exceptions Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of the partial list index operator `!!`. Valid indices return the element; out-of-bounds and negative indices raise exceptions. ```haskell >>> **['a', 'b', 'c'] !! 0 **'a' ``` ```haskell >>> **['a', 'b', 'c'] !! 2 **'c' ``` ```haskell >>> **['a', 'b', 'c'] !! 3 ***** Exception: Prelude.!!: index too large ``` ```haskell >>> **['a', 'b', 'c'] !! (-1) ***** Exception: Prelude.!!: negative index ``` -------------------------------- ### sortOn examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of using sortOn with a projection function. The first sorts a list of tuples by their first element, the second sorts a list of strings by their length. ```haskell >>> **sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] **[(1,"Hello"),(2,"world"),(4,"!")] ``` ```haskell >>> **sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"] **["jim","pam","creed","kevin","dwight","michael"] ``` -------------------------------- ### or examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for or, which returns the disjunction of a container of Bools. It returns True if a True occurs finitely far from the left, but hangs on an infinite list of False. ```haskell >>> **or [] **False ``` ```haskell >>> **or [True] **True ``` ```haskell >>> **or [False] **False ``` ```haskell >>> **or [True, True, False] **True ``` ```haskell >>> **or (True : repeat False) -- Infinite list [True,False,False,False,... **True ``` ```haskell >>> **or (repeat False) *** Hangs forever * ``` -------------------------------- ### intercalate usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of intercalate inserting a list between lists and concatenating the result. The last example shows that empty inner lists still produce the separator between them. ```haskell >>> intercalate ", " ["Lorem", "ipsum", "dolor"] "Lorem, ipsum, dolor" ``` ```haskell >>> intercalate [0, 1] [[2, 3], [4, 5, 6], []] [2,3,0,1,4,5,6,0,1] ``` ```haskell >>> intercalate [1, 2, 3] [[], []] [1,2,3] ``` -------------------------------- ### filter examples: odd, length, and inequality predicates Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of `filter` with different predicates: odd numbers, string length, and inequality. Each shows the filtered list. ```haskell >>> **filter odd [1, 2, 3] **[1,3] ``` ```haskell >>> **filter (\l -> length l > 3) ["Hello", ", ", "World", "!"] **["Hello","World"] ``` ```haskell >>> **filter (/= 3) [1, 2, 3, 4, 3, 2, 1] **[1,2,4,2,1] ``` -------------------------------- ### notElem examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of notElem, the negation of elem. For infinite structures, it terminates if the value exists at a finite distance from the left side; otherwise it hangs. ```haskell >>> **3 `notElem` [] **True ``` ```haskell >>> **3 `notElem` [1,2] **True ``` ```haskell >>> **3 `notElem` [1,2,3,4,5] **False ``` ```haskell >>> **3 `notElem` [1..] **False ``` ```haskell >>> **3 `notElem` ([4..] ++ [3]) *** Hangs forever * ``` -------------------------------- ### foldl1 subtraction example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of foldl1 with subtraction, yielding -8. ```haskell >>> **foldl1 (-) [1..4] **-8 ``` -------------------------------- ### unlines example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example of the unlines function, which appends a newline to each string and concatenates them. ```haskell >>> **unlines ["Hello", "World", "!"] **"Hello\nWorld\n!\n" ``` -------------------------------- ### dropWhileEnd examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of dropWhileEnd with isSpace and a numeric predicate, showing the suffix dropped. ```haskell >>> **dropWhileEnd isSpace "foo\n" **"foo" ``` ```haskell >>> **dropWhileEnd isSpace "foo bar" **"foo bar" >>> **dropWhileEnd (> 10) [1..20] **[1,2,3,4,5,6,7,8,9,10] ``` -------------------------------- ### inits1 :: [a] -> [NonEmpty a] Source: https://hackage.haskell.org/package/base/docs/Data-List.html The inits1 function returns all non-empty initial segments of a list, shortest first. ```APIDOC ## inits1 :: [a] -> [NonEmpty a] ### Description The `inits1` function returns all non-empty initial segments of the argument, shortest first. ### Signature ```haskell inits1 :: [a] -> [NonEmpty a] ``` ### Laziness Note that `inits1` has the following strictness property: `inits1 (xs ++ _|_) = inits1 xs ++ _|_` In particular, `inits1 _|_ = _|_` ### Examples ```haskell >>> inits1 "abc" ['a' :| "",'a' :| "b",'a' :| "bc"] >>> inits1 [] [] ``` `inits1` is productive on infinite lists: ```haskell >>> take 3 $ inits1 [1..] [1 :| [],1 :| [2],1 :| [2,3]] ``` _Since: base-4.21.0.0_ ``` -------------------------------- ### nubBy examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of nubBy, which removes duplicate elements using a user-supplied equality predicate. ```haskell >>> **nubBy (\x y -> mod x 3 == mod y 3) [1,2,4,5,6] **[1,2,6] ``` ```haskell >>> **nubBy (/=) [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] **[2,2,2] ``` ```haskell >>> **nubBy (>) [1, 2, 3, 2, 1, 5, 4, 5, 3, 2] **[1,2,3,5,5] ``` -------------------------------- ### unlines . lines example Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example showing that unlines . lines is not the identity function when the input is not newline-terminated. ```haskell >>> **unlines . lines $ "foo\nbar" **"foo\nbar\n" ``` -------------------------------- ### inits :: [a] -> [[a]] Source: https://hackage.haskell.org/package/base/docs/Data-List.html The inits function returns all initial segments of a list, shortest first. ```APIDOC ## inits :: [a] -> [[a]] ### Description The `inits` function returns all initial segments of the argument, shortest first. `inits` is semantically equivalent to ``map` `reverse` . `scanl` (`flip` (:)) []`, but under the hood uses a queue to amortize costs of `reverse`. ### Signature ```haskell inits :: [a] -> [[a]] ``` ### Laziness Note that `inits` has the following strictness property: `inits (xs ++ _|_) = inits xs ++ _|_` In particular, `inits _|_ = [] : _|_` ### Examples ```haskell >>> inits "abc" ["","a","ab","abc"] >>> inits [] [[]] ``` `inits` is productive on infinite lists: ```haskell >>> take 5 $ inits [1..] [[],[1],[1,2],[1,2,3],[1,2,3,4]] ``` ``` -------------------------------- ### find examples: matching and non-matching predicates Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples for the `find` function from Data.List. The first returns `Just 45` when the predicate matches an element; the second returns `Nothing` when no element satisfies the predicate. ```haskell >>> **find (> 42) [0, 5..] **Just 45 ``` ```haskell >>> **find (> 12) [1..7] **Nothing ``` -------------------------------- ### minimum examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Shows the behavior of minimum on a non-empty list, an empty list, and Nothing. The empty cases raise runtime exceptions. ```haskell >>> minimum [1..10] 1 ``` ```haskell >>> minimum [] *** Exception: Prelude.minimum: empty list ``` ```haskell >>> minimum Nothing *** Exception: minimum: empty structure ``` -------------------------------- ### deleteBy examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of deleteBy, which removes the first occurrence of an element using a user-supplied equality predicate. ```haskell >>> **deleteBy (<=) 4 [1..10] **[1,2,3,5,6,7,8,9,10] ``` ```haskell >>> **deleteBy (/=) 5 [5, 5, 4, 3, 5, 2] **[5,5,3,5,2] ``` -------------------------------- ### subsequences on infinite input Source: https://hackage.haskell.org/package/base/docs/Data-List.html Example showing that subsequences is productive on infinite inputs. ```haskell >>> take 8 $ subsequences ['a'..] ["","a","b","ab","c","ac","bc","abc"] ``` -------------------------------- ### unwords and words inverse examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples showing that unwords is neither left nor right inverse of words. ```haskell >>> **words (unwords [" "]) **[] >>> **unwords (words "foo\nbar") **"foo bar" ``` -------------------------------- ### dropWhile examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of dropWhile with different predicates, showing the suffix after the longest prefix satisfying the predicate. ```haskell >>> **dropWhile (< 3) [1,2,3,4,5,1,2,3] **[3,4,5,1,2,3] ``` ```haskell >>> **dropWhile (< 9) [1,2,3] **[] ``` ```haskell >>> **dropWhile (< 0) [1,2,3] **[1,2,3] ``` -------------------------------- ### singleton :: a -> [a] Source: https://hackage.haskell.org/package/base/docs/Data-List.html Construct a list from a single element. ```APIDOC ## singleton :: a -> [a] ### Description Construct a list from a single element. ### Signature singleton :: a -> [a] ### Parameters - **element** (a) - Required - The element to put in the list. ### Return Value - **[a]** - A list containing the single element. ### Examples ``` >>> singleton True [True] >>> singleton [1, 2, 3] [[1,2,3]] >>> singleton 'c' "c" ``` ``` -------------------------------- ### intersperse usage examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of intersperse inserting an element between the elements of a list, for both character and numeric lists. ```haskell >>> intersperse ',' "abcde" "a,b,c,d,e" ``` ```haskell >>> intersperse 1 [3, 4, 5] [3,1,4,1,5] ``` -------------------------------- ### Show [a] instance Source: https://hackage.haskell.org/package/base/docs/Data-List.html Provides string representation of lists. Available when the element type is an instance of Show. ```APIDOC ## Show [a] instance ### Description Provides string representation of lists. Available when the element type is an instance of Show. ### Methods - **showsPrec :: Int -> [a] -> ShowS** - Converts a list to a show function with precedence. - **show :: [a] -> String** - Converts a list to a string. - **showList :: [[a]] -> ShowS** - Converts a list of lists to a show function. ``` -------------------------------- ### isPrefixOf examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Examples of isPrefixOf, which checks if the first list is a prefix of the second. The first list must be finite for a True result; any mismatch yields False. Infinite first lists cause the function to hang, except when the first list is empty, which returns True immediately. ```haskell >>> **"Hello" `isPrefixOf` "Hello World!" **True ``` ```haskell >>> **"Hello" `isPrefixOf` "Wello Horld!" **False ``` ```haskell >>> **[0..] `isPrefixOf` [1..] **False ``` ```haskell >>> **[0..] `isPrefixOf` [0..99] **False ``` ```haskell >>> **[0..99] `isPrefixOf` [0..] **True ``` ```haskell >>> **[0..] `isPrefixOf` [0..] *** Hangs forever * ``` ```haskell >>> **isPrefixOf [] undefined **True ``` -------------------------------- ### splitAt laziness examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Demonstrates that the first component of splitAt is produced lazily. ```haskell >>> **fst (splitAt 0 undefined) **[] ``` ```haskell >>> **take 1 (fst (splitAt 10 (1 : undefined))) **[1] ``` -------------------------------- ### maximum examples Source: https://hackage.haskell.org/package/base/docs/Data-List.html Shows the behavior of maximum on a non-empty list, an empty list, and Nothing. The empty cases raise runtime exceptions. ```haskell >>> maximum [1..10] 10 ``` ```haskell >>> maximum [] *** Exception: Prelude.maximum: empty list ``` ```haskell >>> maximum Nothing *** Exception: maximum: empty structure ```