In this appendix we present some of the most commonly used definitions from the Haskell standard prelude. For expository purposes, a number of the definitions are presented in simplified form. The full version of the prelude is available from the Haskell home page, http://www.haskell.org.
Basic classes
Equality types:
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)
Ordered types:
class Eq a => Ord a where
(<), (<=), (>), (>=) :: a -> a -> Bool
min, max :: a -> a -> a
min x y | x <= y = x
| otherwise = y
max x y | x <= y = y
| otherwise = x
Showable types:
class Show a where
show :: a -> String
Readable types:
class Read a where
read :: String -> a
Numeric types:
class Num a where
(+), (-), (*) :: a -> a -> a
negate, abs, signum :: a -> a
Integral types:
class Num a => Integral a where
div, mod :: a -> a -> a
Fractional types:
class Num a => Fractional a where
(/) :: a -> a -> a
recip :: a -> a
recip n = 1/n
Booleans
Type declaration:
data Bool = False | True
deriving (Eq, Ord, Show, Read)
Logical conjunction:
(&&) :: Bool -> Bool -> Bool
False && _ = False
True && b = b
Logical disjunction:
(||) :: Bool -> Bool -> Bool
False || b = b
True || _ = True
Logical negation:
not :: Bool -> Bool
not False = True
not True = False
Guard that always succeeds:
otherwise :: Bool
otherwise = True
Characters
Type declaration:
data Char = …
deriving (Eq, Ord, Show, Read)
The definitions below are provided in the library Data.Char, which can be loaded by entering the following in GHCi or at the start of a script:
import Data.Char
Decide if a character is a lower-case letter:
isLower :: Char -> Bool
isLower c = c >= ’a’ && c <= ’z’
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.