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 e
Instances:
All
basic types except functions
Minimal
definition:
either (==)
or
(/=)
(==), (/=) :: e → e → Bool
class Ord o (extends Eq)
Instances:
All basic types except functions
Minimal definition:
either compare
or (<=
)
(<), (<=), (>), (>=) :: o →
o → Bool
compare :: o -> o -> Ordering
Ordering
(i.e. LT
,
EQ
, or GT
).min, max :: o → o → o
class Show s
Instances: All basic types except functions
show
:: s
→
String
class Read r
Instances: All basic types except functions
read :: String → r
class Bounded b
Instances:
Bool
,
Char
,
Int
minBound :: b
maxBound :: b
class Enum e
Instances: Bool
, Char
,
Int
, Double
, Float
An enumerable type that can be used in ranges such as [1 .. 5].
succ :: e -> e
pred :: e -> e
class Num n
Instances: Int
, Integer
,
Float
, Double
(+), (-), (*) :: n → n → n
abs :: n → n
class Integral i (extends Num)
Instances: Int
, Integer
div, mod :: i → i → i
class Fractional f (extends Num)
Instances: Float
, Double
(/) :: f → f → f
cos :: f → f
exp :: f → f
log :: f → f
pi
:: f
sin :: f → f
sqrt :: f → f
tan :: f → f
class RealFrac r (extends Fractional)
Instances:
Float,
Double
ceiling :: Integral i => r → i
floor :: Integral i => r → i
type Int
Instance of: Num
, Integral
A fixed-length signed integer. In GHC, this is a 64-bit value.
type Integer
Instance of: Num
, Integral
A signed integer of arbitrary size.
type Float
type Double
Instance of: Num
, Fractional
Single- and double-precision floating-point numbers.
(^) :: (Num n, Integral i) => n → i →
n
fromIntegral :: (Integral i, Num n) => i → n
type Bool
Instance of: Bounded
A value that is either True
or False
.
(&&) :: Bool → Bool →
Bool
(||) :: Bool → Bool → Bool
not :: Bool → Bool
type Char
Instance of: Bounded
A Unicode character.
Import the Data.Char
module to
access these functions:
chr :: Int → Char
chr(65)
is 'A'
.digitToInt :: Char -> Int
Char
to the corresponding Int
.
For example, (digitToInt '7') == 7
. Hexadecimal digits
are supported.intToDigit :: Int -> Char
Int
in the range 0..15 to the corresponding
Char
. For example, (intToDigit 7) == '7'
.
Hexadecimal digits are supported.isAlpha :: Char → Bool
isDigit :: Char → Bool
isLower :: Char → Bool
isSpace :: Char → Bool
isUpper :: Char → Bool
ord :: Char → Int
ord('A')
is 65
.toLower :: Char → Char
toUpper :: Char → Char
type String = [Char]
A string is a list of characters.
lines :: String → [String]
un
line
s
:: [String] -> String
unwords :: [String] -> String
words :: String → [String]
type (a, b)
type (a, b, c)
…
fst :: (a, b) → a
snd :: (a, b) →
b
data Maybe t = Nothing | Just t
Import the Data.Maybe
module
to access these functions:
fromJust :: Maybe a -> a
Just
, throwing an error if the
argument is Nothing
.fromMaybe :: a -> Maybe a -> a
Maybe
, 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 → a
all :: (a → Bool) → [a] → Bool
and :: [Bool] → Bool
any :: (a → Bool) → [a] → Bool
concat :: [[a]] → [a]
concatMap :: (a -> [b]) -> [a] → [b]
drop :: Int → [a] → [a]
dropWhile :: (a → Bool) → [a] → [a]
elem :: Eq a => a → [a] → Bool
filter :: (a → Bool) → [a] → [a]
foldl :: (a → b → a) → a → [b] → a
foldl (+) 0 [1, 2, 3] == 6
foldl1 :: (a → a → a) → [a] → a
foldl
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] → a
foldr
that has no starting value argument
(and so it can't be applied to empty lists).head :: [a] → a
init :: [a] -> [a]
iterate :: (a -> a) -> a -> [a]
iterate f x = [x, f x, f (f x), ...]
last :: [a] → a
length :: [a] → Int
lookup :: Eq a => a -> [(a, b)] -> Maybe b
map :: (a → b) → [a] → [b]
maximum :: Ord a => [a] → a
minimum :: Ord a => [a] → a
null :: [a] → Bool
or :: [Bool] → Bool
repeat :: 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] → a
tail :: [a] → a
take :: 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 Int
Nothing
if there is no
such element.find :: (a → Bool) → [a] → Maybe a
Nothing
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] -> Bool
isPrefixOf :: Eq a => [a] -> [a] -> Bool
isSuffixOf :: Eq a => [a] -> [a] -> Bool
maximumBy :: (a -> a -> Ordering) -> [a] -> a
minimumBy :: (a -> a -> Ordering) -> [a] -> a
nub :: 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 -> a
Import 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 a
An I/O action that returns a value of type a.
getLine :: IO String
mapM :: (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 a
sequence :: [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 String
writeFile :: 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 r
Instances:
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