This page lists classes, types and functions that we will often use in our course. For the complete story, see the full library documentation at haskell.org.
Quick jump: Type classes | Numbers | Booleans | Characters | Strings | Tuples | Maybe | Lists (extra) | Functions | Ordering | Input/output | Printf | Paths | Files | Directories | Random | Word | Bytestrings | Environment | Miscellaneous
class Eq eInstances:
All
basic types except functions
Minimal
definition:
either (==)
or
(/=)
(==), (/=) :: e → e → Boolclass Ord o (extends Eq)Instances:
All basic types except functions
Minimal definition:
either compare or (<=)
(<), (<=), (>), (>=) :: o →
o → Boolcompare :: o -> o -> OrderingOrdering (i.e. LT,
EQ, or GT).min, max :: o → o → oclass Show sInstances: All basic types except functions
show
:: s →
Stringclass Read rInstances: All basic types except functions
read :: String → rclass Bounded bInstances:
Bool,
Char,
Int
minBound :: b
maxBound :: bclass Enum eInstances: Bool, Char,
Int, Double, Float
An enumerable type that can be used in ranges such as [1 .. 5].
succ :: e -> e
pred :: e -> eclass Num nInstances: Int, Integer,
Float, Double
(+), (-), (*) :: n → n → nabs :: n → nclass Integral i (extends Num)Instances: Int, Integer
div, mod :: i → i → iclass Fractional f (extends Num)Instances: Float, Double
(/) :: f → f → fcos :: f → f
exp :: f → f
log :: f → f
pi
:: f
sin :: f → f
sqrt :: f → f
tan :: f → fclass RealFrac r (extends Fractional)Instances:
Float,
Double
ceiling :: Integral i => r → ifloor :: Integral i => r → itype IntInstance of: Num, Integral
A fixed-length signed integer. In GHC, this is a 64-bit value.
type IntegerInstance of: Num, Integral
A signed integer of arbitrary size.
type Float
type DoubleInstance of: Num, Fractional
Single- and double-precision floating-point numbers.
(^) :: (Num n, Integral i) => n → i →
nfromIntegral :: (Integral i, Num n) => i → ntype BoolInstance of: Bounded
A value that is either True or False.
(&&) :: Bool → Bool →
Bool
(||) :: Bool → Bool → Boolnot :: Bool → Booltype CharInstance of: Bounded
A Unicode character.
Import the Data.Char module to
access these functions:
chr :: Int → Charchr(65)
is 'A'.digitToInt :: Char -> IntChar to the corresponding Int.
For example, (digitToInt '7') == 7. Hexadecimal digits
are supported.intToDigit :: Int -> CharInt in the range 0..15 to the corresponding
Char. For example, (intToDigit 7) == '7'.
Hexadecimal digits are supported.isAlpha :: Char → BoolisDigit :: Char → BoolisLower :: Char → BoolisSpace :: Char → BoolisUpper :: Char → Boolord :: Char → Intord('A')
is 65.toLower :: Char → ChartoUpper :: Char → Chartype String = [Char]A string is a list of characters.
lines :: String → [String]unlines
:: [String] -> Stringunwords :: [String] -> Stringwords :: String → [String]type (a, b)
type (a, b, c)
…fst :: (a, b) → a
snd :: (a, b) →
bdata Maybe t = Nothing | Just t
Import the Data.Maybe module
to access these functions:
fromJust :: Maybe a -> aJust, throwing an error if the
argument is Nothing.fromMaybe :: a -> Maybe a -> aMaybe, return the default
value if the Maybe is Nothing; otherwise
return the value in the Maybe.Just a) where a is the first
element of the list, or Nothing on an empty list.type [a]A list of elements of type a.
Lists can be compared lexicographically.
[] :: [a](:) :: a → [a] → [a](++) :: [a] → [a] → [a](!!) :: [a] → Int → aall :: (a → Bool) → [a] → Booland :: [Bool] → Boolany :: (a → Bool) → [a] → Boolconcat :: [[a]] → [a]concatMap :: (a -> [b]) -> [a] → [b]drop :: Int → [a] → [a]dropWhile :: (a → Bool) → [a] → [a]elem :: Eq a => a → [a] → Boolfilter :: (a → Bool) → [a] → [a]foldl :: (a → b → a) → a → [b] → a
foldl (+) 0 [1, 2, 3] == 6
foldl1 :: (a → a → a) → [a] → afoldl that has no starting value argument
(and so it can't be applied to empty lists).foldr :: (a → b → b) → b → [a] → b
foldr (+) 0 [1, 2, 3] == 6
foldr1 :: (a → a → a) → [a] → afoldr that has no starting value argument
(and so it can't be applied to empty lists).head :: [a] → ainit :: [a] -> [a]iterate :: (a -> a) -> a -> [a]
iterate f x = [x, f x, f (f x), ...]
last :: [a] → alength :: [a] → Intlookup :: Eq a => a -> [(a, b)] -> Maybe bmap :: (a → b) → [a] → [b]maximum :: Ord a => [a] → aminimum :: Ord a => [a] → anull :: [a] → Boolor :: [Bool] → Boolrepeat :: a → [a]replicate :: Int → a → [a]reverse :: [a] → [a]span :: (a -> Bool) -> [a] -> ([a], [a])(span p l)
is equivalent to (takeWhile p l, dropWhile p l).splitAt :: Int -> [a] -> ([a], [a])(splitAt
n xs) is equivalent to (take n xs, drop n xs).sum :: Num a => [a] → atail :: [a] → atake :: Int → [a] → [a]takeWhile :: (a → Bool) → [a] → [a]unzip :: [(a, b)] -> ([a], [b])unzip3 :: [(a, b, c)] -> ([a], [b], [c])zip :: [a] → [b] → [(a, b)]zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]zip3 takes three lists and returns a list of triples.zipWith :: (a → b → c) → [a] → [b] → [c]zip that combines
corresponding elements using the given function rather than a
tupling function.Import the Data.List module to
access these functions:
(\\) :: Eq a => [a] -> [a] -> [a]delete :: Eq a => a -> [a] -> [a]> delete 7 [2, 3, 7, 1, 5] [2,3,1,5] > delete 6 [2, 3, 7, 1, 5] [2,3,7,1,5]
elemIndex :: Eq a => a -> [a] -> Maybe IntNothing if there is no
such element.find :: (a → Bool) → [a] → Maybe aNothing if there is none.group :: Eq a => [a] -> [[a]]
group "Mississippi" ==
["M","i","ss","i","ss","i","pp","i"]
groupBy :: (a
-> a -> Bool) ->
[a] -> [[a]]group,
but using a caller-specified equality test rather than (==).intercalate :: [a] -> [[a]] -> [a]intercalate ", " ["abc",
"def", "ghi"] returns "abc,
def, ghi".intersect :: Eq a => [a] -> [a] -> [a]isInfixOf :: Eq a => [a] -> [a] -> BoolisPrefixOf :: Eq a => [a] -> [a] -> BoolisSuffixOf :: Eq a => [a] -> [a] -> BoolmaximumBy :: (a -> a -> Ordering) -> [a] -> aminimumBy :: (a -> a -> Ordering) -> [a] -> anub :: Eq a => [a] -> [a]partition :: (a -> Bool) -> [a] -> ([a], [a])
(partition p list) ==
(filter p list, filter (\x -> not (p x)) list)
sort :: Ord a => [a] → [a]sortBy :: (a -> a -> Ordering) -> [a] -> [a]tails :: [a]
-> [[a]]
tails
"abc" == ["abc", "bc", "c",
""]
transpose :: [[a]] -> [[a]]transpose [[1,2,3],[4,5,6]] returns
[[1,4],[2,5],[3,6]].union :: Eq a => [a] -> [a] -> [a](.) :: (b → c) → (a → b) → (a →
c)($) :: (a -> b) -> a -> b$ has low precedence
and binds to the right, so
f $ g $ h x == f (g (h x))
id :: a -> aImport the Data.Ord module to get
this function:
comparing p x y = compare (p x) (p y)
maximumBy
and sortBy. For example,sortBy (comparing snd) list
type IO aAn I/O action that returns a value of type a.
getLine :: IO StringmapM :: (a -> IO b) -> [a] -> IO [b]mapM_ :: (a -> IO b) -> [a] -> IO ()mapM, but ignores the resulting values.print :: Show a => a -> IO ()putStr :: String → IO ()putStrLn :: String → IO ()readLn :: Read a => IO asequence :: [IO a] -> IO [a]Import the Control.Monad
module to access these functions:
forM :: [a] -> (a -> IO b) -> IO
[b]mapM, but with the arguments flipped.forM_ :: [a] -> (a -> IO b) -> IO ()mapM_, but with the arguments flipped.when :: Bool -> IO () -> IO ()Bool
is true, then when returns the same I/O
action it is given. If the given Bool is false,
then when returns the action return (),
which does nothing.Import the Text.Printf module to access this function:
>>> printf "%s, %d, %.4f" "hello" 123 pi hello, 123, 3.1416
printf, such as in the standard C library. This
version is largely
compatible; for example, in the format string above, %s represents a
string, %d represents a integer and %.4f is a floating-point number
rounded to four digits after the decimal point. See the official
documentation for more details.type FilePath = String
A FilePath represents a filename.
Import the System.FilePath
module to access these functions:
</> is a
synonym for the combine function. For example,"mydir" </> "myfile.txt""mydir/myfile.txt" on Unix or
macOS, and "mydir\myfile.txt" on Windows.takeExtension
"mydir/myfile.png" will return ".png".readFile :: FilePath -> IO StringwriteFile :: FilePath -> String -> IO ()Import the System.Directory
module to access these functions:
True if the given path exists and is a directory
(i.e. not a file), otherwise False.True if the given path exists and is a file
(i.e. not a directory), otherwise False.To access these functions, you must install the
random package and import the System.Random
module.
class RandomGen g
A random number generator.
next :: g -> (Int, g)
Return a random Int
in a certain range, plus a new generator.
split :: g -> (g, g)
Split a random number generator into two generators.
data StdGen
The standard random number generator implementation.
mkStdGen
:: Int -> StdGen
Make
a StdGen
given a seed value.
newStdGen
:: IO StdGen
Return a new random number generator with a system-dependent seed.
class Random rInstances:
All built-in numeric types,
Bool, Char
A type that can be generated randomly.
random :: RandomGen g => g -> (r, g)
Generate a random value, together with a new generator. For bounded types, the range is normally the whole type. For fractional types, the range is normally the interval [0, 1).
randomR :: RandomGen g => (r, r) -> g
-> (r, g)
Given a range (lo, hi) and a generator, returns a value in the interval [low, hi], together with a new generator.
randomRs :: RandomGen g => (r, r) -> g
-> [r]
A plural variant of
randomR, producing an infinite list of random values
instead of returning a new generator.
randoms :: RandomGen g => g -> [r]
A plural variant of
random, producing an infinite list of random values
instead of returning a new generator.
Import the Data.Word module to access
this type:
Bytestrings are sequences of bytes.
Import
the Data.ByteString or
Data.ByteString.Lazy module to access the
following functions. (You probably want to use a qualified import
to avoid name clashes with the Data.List module.)
ByteString.ByteString modules additionally contain many
functions with the same names and functionality as their
counterparts in the Data.List module. For details, see
the official
documentation for ByteString.Import the System.Environment
module to access this function:
getArgs :: IO [String]
Return a list of the program's command-line arguments.
error :: String -> a