Real World Haskell - 第4章 Part1 like a "laundry list"

Real World HaskellHaskell を継続的に勉強中。


Chapter 4. Functional programming
Chapter 4. Functional programming

Infix functions

  • infix notations is purely a syntactic convenience

Woking with lists

  • :module +Data.List
  • length :: [a] -> Int
    • 全要素を参照するので不用意に使うと無限ループになる
  • null :: [a] -> Bool
  • head :: [a] -> a
  • tail :: [a] -> [a]
  • last :: [a] -> a
  • init :: [a] -> [a]

Partial and total functions

Functions that only have return values defined for a subset of valid inputs are called partial functions (calling error doesn't qualify as returning a value!). We call functions that return valid results over their entire input domains total functions.

  • head などは partial だけど unsafe と付いていない

More simple list manipulations

  • (++) :: [a] -> [a] -> [a]
    • append
  • concat :: a -> [a]
  • reverse :: [a] -> [a]
  • and :: [Bool] -> Bool
  • or :: [Bool] -> Bool
  • all :: (a -> Bool) -> [a] -> Bool
  • any :: (a -> Bool) -> [a] -> Bool

Wroking with sublists

  • take :: Int -> [a] -> [a]
  • drop :: Int -> [a] -> [a]
  • splitAt :: Int -> [a] -> ([a], [a])
  • takeWhile :: (a -> Bool) -> [a] -> [a]
  • dropWhile :: (a -> Bool) -> [a] -> [a]
  • span :: (a -> Bool) -> [a] -> ([a], [a])
  • break :: (a -> Bool) -> [a] -> ([a], [a])

Searching lists

  • elem :: (Eq a) => a -> [a] -> Bool
  • filter :: (a -> Bool) -> [a] -> [a]
  • :module +Data.List
    • isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
    • isInfixOf :: (Eq a) => [a] -> [a] -> Bool
    • isSuffixOf :: (Eq a) => [a] -> [a] -> Bool

Working with several lists at once

  • zip :: [a] -> [b] -> [(a, b)]
    • zip7 まである
  • zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    • zipWith7 まである
Prelude> :module +Data.List
Prelude Data.List> :type zip7
zip7 :: [a]
        -> [b]
        -> [c]
        -> [d]
        -> [e]
        -> [f]
        -> [g]
        -> [(a, b, c, d, e, f, g)]
Prelude Data.List> :type zipWith7
zipWith7 :: (a -> b -> c -> d -> e -> f -> g -> h)
            -> [a]
            -> [b]
            -> [c]
            -> [d]
            -> [e]
            -> [f]
            -> [g]
            -> [h]
Prelude Data.List>

Special string-handling functions

  • lines :: String -> [String]
  • unlines :: [String] -> String
  • words :: String -> [String]
  • unwords :: [String] -> String