### all examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the all function with a predicate, including an infinite list that hangs. ```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 * ``` -------------------------------- ### unwords examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples for the unwords function, showing that it joins words with spaces and is not an inverse of words, plus examples with empty strings. ```haskell >>> **words (unwords [" "]) **[] >>> **unwords (words "foo\nbar") **"foo bar" ``` ```haskell >>> **unwords ["Lorem", "ipsum", "dolor"] **"Lorem ipsum dolor" ``` ```haskell >>> **unwords ["foo", "bar", "", "baz"] **"foo bar baz" ``` -------------------------------- ### scanl definition and examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Definition and examples of scanl, including a lambda example and an infinite list. ```haskell scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] ``` ```haskell last (scanl f z xs) == foldl f z xs ``` ```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" ``` -------------------------------- ### iterate examples Source: https://hackage.haskell.org/package/base/docs/Prelude.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] ``` -------------------------------- ### any examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the any function with a predicate, including an infinite list that hangs. ```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 * ``` -------------------------------- ### (++) example with two lists Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of appending two finite lists. ```haskell >>> **[1, 2, 3] ++ [4, 5, 6] **[1,2,3,4,5,6] ``` -------------------------------- ### unzip3 examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples for the unzip3 function, showing the result for an empty list and a non-empty list of triples. ```haskell >>> **unzip3 [] **([],[],[]) ``` ```haskell >>> **unzip3 [(1, 'a', True), (2, 'b', False)] **([1,2],"ab",[True,False]) ``` -------------------------------- ### readFile example: read a file Source: https://hackage.haskell.org/package/base/docs/Prelude.html Reads a file lazily and returns its contents as a string. The example shows reading a file named "~/hello_world" and getting the string "Greetings!". This operation may fail with the same errors as `hGetContents` and `openFile`. ```haskell >>> **readFile "~/hello_world" **"Greetings!" ``` -------------------------------- ### concat examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the concat function on containers 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] ``` -------------------------------- ### words examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples for the words function, showing how it splits on whitespace and trims leading/trailing spaces. ```haskell >>> **words "Lorem ipsum\ndolor" **["Lorem","ipsum","dolor"] ``` ```haskell >>> **words " foo bar " **["foo","bar"] ``` -------------------------------- ### and examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the and function on Bools, including an infinite list that hangs. ```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 * ``` -------------------------------- ### unzip examples: empty and non-empty lists Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of `unzip` on an empty list and a list of pairs, returning a pair of lists. ```haskell >>> unzip [] ([],[]) ``` ```haskell >>> unzip [(1, 'a'), (2, 'b')] ([1,2],"ab") ``` -------------------------------- ### take examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of take: returns the prefix of length n, or the whole list if n is larger. Negative or zero n yields an empty list. ```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/Prelude.html Examples of repeat: creates an infinite list with the same value. The second example shows that evaluating repeat undefined throws an exception. ```haskell >>> **take 10 $ repeat 17 **[17,17,17,17,17,17,17,17,17, 17] ``` ```haskell >>> **repeat undefined **[*** Exception: Prelude.undefined ``` -------------------------------- ### head example on empty list (exception) Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of head on an empty list, which throws an exception because head is partial. ```haskell >>> **head [] ***** Exception: Prelude.head: empty list ``` -------------------------------- ### cycle examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of cycle: ties a finite list into a circular one. Empty list throws 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] ``` -------------------------------- ### (++) example with empty first list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of appending an empty list to a non-empty list. ```haskell >>> **[] ++ [1, 2, 3] **[1,2,3] ``` -------------------------------- ### or examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the or function on Bools, including an infinite list that hangs. ```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 * ``` -------------------------------- ### concatMap examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the concatMap function mapping and concatenating 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] ``` -------------------------------- ### replicate examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of replicate: returns 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] ``` -------------------------------- ### tail example on empty list (exception) Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of tail on an empty list, which throws an exception because tail is partial. ```haskell >>> **tail [] ***** Exception: Prelude.tail: empty list ``` -------------------------------- ### sequenceA examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples for the sequenceA method of the Traversable class, showing full evaluation and short-circuiting with Nothing and Left. ```haskell >>> **sequenceA [Just 1, Just 2, Just 3] **Just [1,2,3] ``` ```haskell >>> **sequenceA [Right 1, Right 2, Right 3] **Right [1,2,3] ``` ```haskell >>> **sequenceA [Just 1, Just 2, Just 3, Nothing] **Nothing ``` ```haskell >>> **sequenceA [Right 1, Right 2, Right 3, Left 4] **Left 4 ``` -------------------------------- ### (++) example with empty second list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of appending an empty list to a non-empty list. ```haskell >>> **[3, 2, 1] ++ [] **[3,2,1] ``` -------------------------------- ### head example on non-empty list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of head returning the first element of a non-empty list. ```haskell >>> **head [1, 2, 3] **1 ``` -------------------------------- ### mconcat example Source: https://hackage.haskell.org/package/base/docs/Prelude.html GHCi example showing mconcat folding a list of strings into a single string. ```haskell >>> mconcat ["Hello", " ", "Haskell", "!"] "Hello Haskell!" ``` -------------------------------- ### head example on infinite list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of head on an infinite list, showing it works because it only needs the first element. ```haskell >>> **head [1..] **1 ``` -------------------------------- ### (.) example: map with composition Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of using composition to double the length of each list. The output is [0,6,2]. ```haskell >>> **map ((*2) . length) [[], [0, 1, 2], [0]] **[0,6,2] ``` -------------------------------- ### reverse examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of the reverse function on lists, including an infinite list that hangs. ```haskell >>> reverse [] [] ``` ```haskell >>> reverse [42] [42] ``` ```haskell >>> reverse [2,5,7] [7,5,2] ``` ```haskell >>> reverse [1..] * Hangs forever * ``` -------------------------------- ### ($) example: section Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of using ($) as a section to apply a value to a list of functions. The output is [6, 32]. ```haskell applyFive :: [Int] applyFive = map ($ 5) [(+1), (2^)] >>> [6, 32] ``` -------------------------------- ### last example on empty list (exception) Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of last on an empty list, which throws an exception because last is partial. ```haskell >>> **last [] ***** Exception: Prelude.last: empty list ``` -------------------------------- ### curry fst example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive GHCi example showing that curry fst applied to 1 and 2 yields 1. ```haskell >>> **curry fst 1 2 **1 ``` -------------------------------- ### drop examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of drop: returns the suffix after the first n elements, or [] if n is larger. Negative n returns the whole list. ```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] ``` -------------------------------- ### foldMap basic usage examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Basic usage examples of foldMap with Sum, Product, and replicate, showing the resulting monoid values. ```haskell >>> **foldMap Sum [1, 3, 5] **Sum {getSum = 9} ``` ```haskell >>> **foldMap Product [1, 3, 5] **Product {getProduct = 15} ``` ```haskell >>> **foldMap (replicate 3) [1, 2, 3] **[1,1,1,2,2,2,3,3,3] ``` -------------------------------- ### tail example on non-empty list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of tail returning the list without its first element. ```haskell >>> **tail [1, 2, 3] **[2,3] ``` -------------------------------- ### tail example on single-element list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of tail on a single-element list, returning an empty list. ```haskell >>> **tail [1] **[] ``` -------------------------------- ### (.) example: foldr with composition Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of folding a list of functions with composition and id as the base. The output is 25. ```haskell >>> **foldr (.) id [(+1), (*3), (^3)] 2 **25 ``` -------------------------------- ### (.) example: composing composition Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of composing composition operators to create a custom operator. The output is 30. ```haskell >>> **let (...) = (.).(.) in ((*2)...(+)) 5 10 **30 ``` -------------------------------- ### const example: basic usage Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of const returning the first argument and ignoring the second. The output is 42. ```haskell >>> **const 42 "hello" **42 ``` -------------------------------- ### uncurry (+) example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive GHCi example showing that uncurry (+) applied to the pair (1,2) yields 3. ```haskell >>> **uncurry (+) (1,2) **3 ``` -------------------------------- ### map (uncurry max) example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive GHCi example showing that mapping uncurry max over a list of pairs yields the list [2,4,8]. ```haskell >>> **map (uncurry max) [(1,2), (3,4), (6,8)] **[2,4,8] ``` -------------------------------- ### traverse examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples for the traverse method of the Traversable class, showing successful collection and short-circuiting with Nothing and Left. ```haskell >>> **traverse Just [1,2,3,4] **Just [1,2,3,4] ``` ```haskell >>> **traverse id [Right 1, Right 2, Right 3, Right 4] **Right [1,2,3,4] ``` ```haskell >>> **traverse (const Nothing) [1,2,3,4] **Nothing ``` ```haskell >>> **traverse (\x -> if odd x then Just x else Nothing) [1,2,3,4] **Nothing ``` ```haskell >>> **traverse id [Right 1, Right 2, Right 3, Right 4, Left 0] **Left 0 ``` -------------------------------- ### unlines examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples for the unlines function, showing that it appends a newline to each string and concatenates, and that unlines . lines is not the identity on unterminated input. ```haskell >>> **unlines ["Hello", "World", "!"] **"Hello\nWorld\n!\n" ``` ```haskell >>> **unlines . lines $ "foo\nbar" **"foo\nbar\n" ``` -------------------------------- ### init examples: non-empty, singleton, and empty list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Shows the behavior of the partial function init on a non-empty list, a singleton list, and an empty list. The empty list case raises the exception Prelude.init: empty list. ```haskell >>> **init [1, 2, 3] **[1,2] ``` ```haskell >>> **init [1] **[] ``` ```haskell >>> **init [] ***** Exception: Prelude.init: empty list ``` -------------------------------- ### last example on infinite list (hangs) Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of last on an infinite list, which hangs forever because it must traverse the entire list. ```haskell >>> **last [1..] *** Hangs forever * ``` -------------------------------- ### uncurry ($) example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive GHCi example showing that uncurry ($) applied to the pair (show, 1) yields the string "1". ```haskell >>> **uncurry ($) (show, 1) **"1" ``` -------------------------------- ### product basic usage examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Basic usage of product with various numeric inputs. An infinite list causes the computation to hang. ```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 * ``` -------------------------------- ### scanr definition and examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Definition and examples of scanr, including a lambda example and a stack overflow on an infinite list. ```haskell head (scanr f z xs) == foldr f z xs. ``` ```haskell >>> scanr (+) 0 [1..4] [10,9,7,4,0] ``` ```haskell >>> scanr (+) 42 [] [42] ``` ```haskell >>> scanr (-) 100 [1..4] [98,-97,99,-96,100] ``` ```haskell >>> scanr (\nextChar reversedString -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd'] ["abcdfoo","bcdfoo","cdfoo","dfoo","foo"] ``` ```haskell >>> force $ scanr (+) 0 [1..] * Exception: stack overflow ``` -------------------------------- ### splitAt examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive GHCi examples showing the behavior of `splitAt` with various indices and lists. They illustrate splitting strings and lists, handling indices larger than the list length, and negative indices. ```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]) ``` -------------------------------- ### lines examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples for the lines function, showing how it splits strings on newline characters, including edge cases for empty input, trailing newlines, and unterminated final lines. ```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"] ``` -------------------------------- ### appendFile example: write, append, and read Source: https://hackage.haskell.org/package/base/docs/Prelude.html Writes "hello" to a file, appends " world!", then reads and prints the result, yielding "hello world!". The example notes that this could be more efficiently written by acquiring a handle with `openFile` and using handle-writing computations such as `hPutStr`. ```haskell >>> **let fn = "hello_world" **>>> **in writeFile fn "hello" >> appendFile fn " world!" >> (readFile fn >>= putStrLn) **"hello world!" ``` -------------------------------- ### scanr1 examples Source: https://hackage.haskell.org/package/base/docs/Prelude.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 ``` -------------------------------- ### IO action sequencing with <> Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of the (<>) operator for IO actions, sequencing two output actions. ```haskell >>> putStr "Hello, " <> putStrLn "World!" Hello, World! ``` -------------------------------- ### readFile example: read first five bytes Source: https://hackage.haskell.org/package/base/docs/Prelude.html Reads the first five bytes of a file using `readFile` and `take`. The example reads from `/dev/zero` and returns a string of five NUL characters. `readFile` reads lazily, on demand, as with `getContents`. ```haskell >>> **take 5 <$> readFile "/dev/zero" **"\NUL\NUL\NUL\NUL\NUL" ``` -------------------------------- ### filter odd example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of filter keeping only odd numbers. ```haskell >>> **filter odd [1, 2, 3] **[1,3] ``` -------------------------------- ### getLine example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Demonstrates getLine reading a line from stdin. The input 'Hello World!' is echoed and the result is the string without the newline. ```haskell >>> getLine **> Hello World! "Hello World!" ``` -------------------------------- ### filter (/= 3) example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of filter removing all occurrences of 3. ```haskell >>> **filter (/= 3) [1, 2, 3, 4, 3, 2, 1] **[1,2,4,2,1] ``` -------------------------------- ### map (+1) example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of map adding 1 to each element of a list. ```haskell >>> **map (+1) [1, 2, 3] **[2,3,4] ``` -------------------------------- ### ($) example: with operator Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of the same function using ($) to avoid parentheses. This is the version with the operator. ```haskell -- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum $ mapMaybe readMaybe $ words s ``` -------------------------------- ### takeWhile examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of takeWhile: returns the longest prefix of elements 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] **[] ``` -------------------------------- ### scanl1 definition and examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Definition and examples of scanl1, including boolean operators and an infinite list. ```haskell scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...] ``` ```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" ``` -------------------------------- ### read function examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Interactive examples showing the behavior of the read function. The first parses a valid integer string, while the second demonstrates the error thrown when parsing fails. ```haskell >>> **read "123" :: Int **123 ``` ```haskell >>> **read "hello" :: Int ***** Exception: Prelude.read: no parse ``` -------------------------------- ### show :: a -> String Source: https://hackage.haskell.org/package/base/docs/Prelude.html A specialised variant of showsPrec, using precedence context zero, and returning an ordinary String. ```APIDOC ## show :: a -> String ### Description A specialised variant of `showsPrec`, using precedence context zero, and returning an ordinary `String`. ### Method show ### Signature show :: a -> String ### Parameters - **a** (type variable) - The value to convert to a String. ### Return Value - **String** - The string representation of the value. ``` -------------------------------- ### map with lambda example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of map with a lambda expression that multiplies by 3 and adds 1. ```haskell >>> **map (\n -> 3 * n + 1) [1, 2, 3] **[4,7,10] ``` -------------------------------- ### readLn example: read and map Source: https://hackage.haskell.org/package/base/docs/Prelude.html Reads a line with `readLn`, parses it, and adds 5. The example shows input `25` and output `30`. `readLn` combines `getLine` and `readIO` and may fail with the same errors as `getLine` and `readIO`. ```haskell >>> **fmap (+ 5) readLn **> 25 30 ``` -------------------------------- ### writeFile example: write and read a file Source: https://hackage.haskell.org/package/base/docs/Prelude.html Writes the string "world" to the file "hello" and then reads it back, returning "world". `writeFile` may fail with the same errors as `hPutStr` and `withFile`. ```haskell >>> **writeFile "hello" "world" >> readFile "hello" **"world" ``` -------------------------------- ### dropWhile examples Source: https://hackage.haskell.org/package/base/docs/Prelude.html Examples of dropWhile: returns the suffix after removing the longest prefix of elements 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] ``` -------------------------------- ### last example on finite list Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of last returning the last element of a finite non-empty list. ```haskell >>> **last [1, 2, 3] **3 ``` -------------------------------- ### filter with length lambda example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of filter with a lambda that checks if the length of a string is greater than 3. ```haskell >>> **filter (\l -> length l > 3) ["Hello", ", ", "World", "!"] **["Hello","World"] ``` -------------------------------- ### map id example Source: https://hackage.haskell.org/package/base/docs/Prelude.html Example of map with the identity function, showing that map id equals id. ```haskell >>> **map id [1, 2, 3] **[1,2,3] ``` -------------------------------- ### Hand-written Read instance with readPrec Source: https://hackage.haskell.org/package/base/docs/Prelude.html Recommended pattern for hand-written Read instances: implement readPrec and use readListPrecDefault for readListPrec. ```haskell instance Read T where readPrec = ... readListPrec = readListPrecDefault ```