Lists, Tuples and Strings
5.1 Lists
nums = [1, 2, 3, 4, 5]
empty = [] :: [Int]
consed = 0 : nums -- (:) "cons" prepends: [0,1,2,3,4,5]
joined = [1, 2] ++ [3, 4] -- concatenation (O(n) in the left list)
range1 = [1 .. 10]
range2 = [2, 4 .. 20] -- step
range3 = ['a' .. 'e'] -- "abcde"
infinite = [1 ..] -- infinite (lazy!)
head nums, tail nums, last nums, init nums -- 1, [2,3,4,5], 5, [1,2,3,4] (head/tail are partial!)
nums !! 2 -- 3 (indexing, O(n), partial)
length nums, null nums, reverse nums
take 3 nums, drop 3 nums, splitAt 2 nums
elem 3 nums, notElem 9 nums
sum nums, product nums, maximum nums, minimum nums
concat [[1, 2], [3]], concatMap (replicate 2) [1, 2] -- [1,2,3], [1,1,2,2]
replicate 3 'x', cycle [1, 2] & take 5, iterate (* 2) 1 & take 5 -- "xxx", [1,2,1,2,1], [1,2,4,8,16]
zip [1, 2, 3] "abc" -- [(1,'a'),(2,'b'),(3,'c')]
zipWith (+) [1, 2] [10, 20] -- [11,22]
unzip [(1, 'a'), (2, 'b')] -- ([1,2],"ab")
lookup 2 [(1, "one"), (2, "two")] -- Just "two"
takeWhile (< 3) nums, dropWhile (< 3) nums, span even [2, 4, 5, 6], break (> 2) nums
and [True, False], or [True, False], any even nums, all (> 0) nums
words "hello big world", unwords ["a", "b
…import Data.List (sort, sortBy, sortOn, nub, group, groupBy, partition, intercalate, transpose,
isPrefixOf, isSuffixOf, isInfixOf, foldl', insert, delete, (\\), union, intersect,
tails, inits, subsequences, permutations, find, findIndex, elemIndex, zip4, unfoldr)
import Data.Ord (comparing, Down (..))
sort [3, 1, 2] -- [1,2,3]
sortOn negate [3, 1, 2] -- [3,2,1]
sortOn Down [3, 1, 2] -- [3,2,1]
sortBy (comparing snd) [(1, 'b'), (2, 'a')]
nub [1, 1, 2, 3, 2] -- [1,2,3] (O(n²); use Data.Set for big lists)
group (sort "mississippi") -- ["iiii","m","pp","ssss"]
partition even [1 .. 10] -- ([2,4,6,8,10],[1,3,5,7,9])
intercalate ", " ["a", "b", "c"] -- "a, b, c"
transpose [[1, 2, 3], [4, 5, 6]] -- [[1,4],[2,5],[3,6]]
"ab" `isPrefixOf` "abc" -- True
find (> 3) [1 .. 10] -- Just 4
permutations [1, 2, 3] -- 6 permutations
unfoldr (\n -> if n > 100 then Nothing else Just (n, n * 2)) 1 -- [1,2,4,8,16,32,64]5.2 List Comprehensions
squares = [x * x | x <- [1 .. 10]]
evens = [x | x <- [1 .. 20], even x]
pairs = [(x, y) | x <- [1 .. 3], y <- "ab"] -- cartesian product
pythag = [(a, b, c) | c <- [1 .. 20], b <- [1 .. c], a <- [1 .. b], a * a + b * b == c * c]
flattened = [x | xs <- [[1, 2], [3]], x <- xs]
withLet = [y | x <- [1 .. 5], let y = x * 10, y > 20]
primes = sieve [2 ..] where sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
-- take 10 primes == [2,3,5,7,11,13,17,19,23,29]5.3 Tuples
pair = (1, "one")
fst pair, snd pair -- 1, "one"
triple = (1, 'a', True)
swap (1, 2) -- (2, 1) (Data.Tuple)
curry fst 1 2 -- 1
uncurry (+) (3, 4) -- 75.4 Strings and Text
String = [Char]: simple, but slow and memory-heavy for real workloads. Use Data.Text for text and Data.ByteString for bytes in real programs (Module 24).
show 42 -- "42" (convert to String)
read "42" :: Int -- 42 (partial: crashes on bad input)
import Text.Read (readMaybe)
readMaybe "42" :: Maybe Int -- Just 42
readMaybe "x" :: Maybe Int -- Nothing
import Data.Char (toUpper, toLower, isDigit, isAlpha, isSpace, ord, chr, digitToInt)
map toUpper "hello" -- "HELLO"
filter isDigit "a1b2" -- "12"