|
| Prelude | | Portability | portable | | Stability | stable | | Maintainer | libraries@haskell.org |
|
|
|
|
|
| Description |
| The Prelude: a standard module imported by default into all Haskell
modules. For more documentation, see the Haskell 98 Report
http://www.haskell.org/onlinereport/.
|
|
| Synopsis |
|
| | | (&&) :: Bool -> Bool -> Bool | | | (||) :: Bool -> Bool -> Bool | | | not :: Bool -> Bool | | | otherwise :: Bool | | | | | maybe :: b -> (a -> b) -> Maybe a -> b | | | | | either :: (a -> c) -> (b -> c) -> Either a b -> c | | | | | data Char | | | type String = [Char] | | | fst :: (a, b) -> a | | | snd :: (a, b) -> b | | | curry :: ((a, b) -> c) -> a -> b -> c | | | uncurry :: (a -> b -> c) -> (a, b) -> c | | | class Eq a where | | | | class Eq a => Ord a where | | | | class Enum a where | | | | class Bounded a where | | | | data Int | | | data Integer | | | data Float | | | data Double | | | type Rational = Ratio Integer | | | class (Eq a, Show a) => Num a where | | | | class (Num a, Ord a) => Real a where | | | | class (Real a, Enum a) => Integral a where | | | | class Num a => Fractional a where | | | | class Fractional a => Floating a where | | | | class (Real a, Fractional a) => RealFrac a where | | | | class (RealFrac a, Floating a) => RealFloat a where | | | | subtract :: Num a => a -> a -> a | | | even :: Integral a => a -> Bool | | | odd :: Integral a => a -> Bool | | | gcd :: Integral a => a -> a -> a | | | lcm :: Integral a => a -> a -> a | | | (^) :: (Num a, Integral b) => a -> b -> a | | | (^^) :: (Fractional a, Integral b) => a -> b -> a | | | fromIntegral :: (Integral a, Num b) => a -> b | | | realToFrac :: (Real a, Fractional b) => a -> b | | | class Monad m where | | | | class Functor f where | | fmap :: (a -> b) -> f a -> f b |
| | | mapM :: Monad m => (a -> m b) -> [a] -> m [b] | | | mapM_ :: Monad m => (a -> m b) -> [a] -> m () | | | sequence :: Monad m => [m a] -> m [a] | | | sequence_ :: Monad m => [m a] -> m () | | | (=<<) :: Monad m => (a -> m b) -> m a -> m b | | | id :: a -> a | | | const :: a -> b -> a | | | (.) :: (b -> c) -> (a -> b) -> a -> c | | | flip :: (a -> b -> c) -> b -> a -> c | | | ($) :: (a -> b) -> a -> b | | | until :: (a -> Bool) -> (a -> a) -> a -> a | | | asTypeOf :: a -> a -> a | | | error :: String -> a | | | undefined :: a | | | seq :: a -> b -> b | | | ($!) :: (a -> b) -> a -> b | | | map :: (a -> b) -> [a] -> [b] | | | (++) :: [a] -> [a] -> [a] | | | filter :: (a -> Bool) -> [a] -> [a] | | | head :: [a] -> a | | | last :: [a] -> a | | | tail :: [a] -> [a] | | | init :: [a] -> [a] | | | null :: [a] -> Bool | | | length :: [a] -> Int | | | (!!) :: [a] -> Int -> a | | | reverse :: [a] -> [a] | | | foldl :: (a -> b -> a) -> a -> [b] -> a | | | foldl1 :: (a -> a -> a) -> [a] -> a | | | foldr :: (a -> b -> b) -> b -> [a] -> b | | | foldr1 :: (a -> a -> a) -> [a] -> a | | | and :: [Bool] -> Bool | | | or :: [Bool] -> Bool | | | any :: (a -> Bool) -> [a] -> Bool | | | all :: (a -> Bool) -> [a] -> Bool | | | sum :: Num a => [a] -> a | | | product :: Num a => [a] -> a | | | concat :: [[a]] -> [a] | | | concatMap :: (a -> [b]) -> [a] -> [b] | | | maximum :: Ord a => [a] -> a | | | minimum :: Ord a => [a] -> a | | | scanl :: (a -> b -> a) -> a -> [b] -> [a] | | | scanl1 :: (a -> a -> a) -> [a] -> [a] | | | scanr :: (a -> b -> b) -> b -> [a] -> [b] | | | scanr1 :: (a -> a -> a) -> [a] -> [a] | | | iterate :: (a -> a) -> a -> [a] | | | repeat :: a -> [a] | | | replicate :: Int -> a -> [a] | | | cycle :: [a] -> [a] | | | 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]) | | | elem :: Eq a => a -> [a] -> Bool | | | notElem :: Eq a => a -> [a] -> Bool | | | lookup :: Eq a => a -> [(a, b)] -> Maybe b | | | zip :: [a] -> [b] -> [(a, b)] | | | zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] | | | zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] | | | zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] | | | unzip :: [(a, b)] -> ([a], [b]) | | | unzip3 :: [(a, b, c)] -> ([a], [b], [c]) | | | lines :: String -> [String] | | | words :: String -> [String] | | | unlines :: [String] -> String | | | unwords :: [String] -> String | | | type ShowS = String -> String | | | class Show a where | | | | shows :: Show a => a -> ShowS | | | showChar :: Char -> ShowS | | | showString :: String -> ShowS | | | showParen :: Bool -> ShowS -> ShowS | | | type ReadS a = String -> [(a, String)] | | | class Read a where | | | | reads :: Read a => ReadS a | | | readParen :: Bool -> ReadS a -> ReadS a | | | read :: Read a => String -> a | | | lex :: ReadS String | | | data IO a | | | putChar :: Char -> IO () | | | putStr :: String -> IO () | | | putStrLn :: String -> IO () | | | print :: Show a => a -> IO () | | | getChar :: IO Char | | | getLine :: IO String | | | getContents :: IO String | | | interact :: (String -> String) -> IO () | | | type FilePath = String | | | readFile :: FilePath -> IO String | | | writeFile :: FilePath -> String -> IO () | | | appendFile :: FilePath -> String -> IO () | | | readIO :: Read a => String -> IO a | | | readLn :: Read a => IO a | | | type IOError = IOException | | | ioError :: IOError -> IO a | | | userError :: String -> IOError | | | catch :: IO a -> (IOError -> IO a) -> IO a |
|
|
|
| Standard types, classes and related functions
|
|
| Basic data types
|
|
|
| The Bool type is an enumeration. It is defined with False
first so that the corresponding Enum instance will give
fromEnum False the value zero, and
fromEnum True the value 1.
| | Constructors | | Instances | |
|
|
|
| Boolean "and"
|
|
|
| Boolean "or"
|
|
|
| Boolean "not"
|
|
|
otherwise is defined as the value True. It helps to make
guards more readable. eg.
f x | x < 0 = ...
| otherwise = ...
|
|
|
The Maybe type encapsulates an optional value. A value of type
Maybe a either contains a value of type a (represented as Just a),
or it is empty (represented as Nothing). Using Maybe is a good way to
deal with errors or exceptional cases without resorting to drastic
measures such as error.
The Maybe type is also a monad. It is a simple kind of error
monad, where all errors are represented by Nothing. A richer
error monad can be built using the Either type.
| | Constructors | | Instances | |
|
|
|
| The maybe function takes a default value, a function, and a Maybe
value. If the Maybe value is Nothing, the function returns the
default value. Otherwise, it applies the function to the value inside
the Just and returns the result.
|
|
|
The Either type represents values with two possibilities: a value of
type Either a b is either Left a or Right b.
The Either type is sometimes used to represent a value which is
either correct or an error; by convention, the Left constructor is
used to hold an error value and the Right constructor is used to
hold a correct value (mnemonic: "right" also means "correct").
| | Constructors | | Instances | |
|
|
|
| Case analysis for the Either type.
If the value is Left a, apply the first function to a;
if it is Right b, apply the second function to b.
|
|
|
| Represents an ordering relationship between two values: less
than, equal to, or greater than. An Ordering is returned by
compare.
| | Constructors | | Instances | |
|
|
|
The character type Char is an enumeration whose values represent
Unicode (or equivalently ISO/IEC 10646) characters
(see http://www.unicode.org/ for details).
This set extends the ISO 8859-1 (Latin-1) character set
(the first 256 charachers), which is itself an extension of the ASCII
character set (the first 128 characters).
A character literal in Haskell has type Char.
To convert a Char to or from the corresponding Int value defined
by Unicode, use toEnum and fromEnum from the
Enum class respectively (or equivalently ord and chr).
| Instances | |
|
|
|
| A String is a list of characters. String constants in Haskell are values
of type String.
|
|
| Tuples
|
|
|
| Extract the first component of a pair.
|
|
|
| Extract the second component of a pair.
|
|
| curry :: ((a, b) -> c) -> a -> b -> c | Source |
|
| curry converts an uncurried function to a curried function.
|
|
| uncurry :: (a -> b -> c) -> (a, b) -> c | Source |
|
| uncurry converts a curried function to a function on pairs.
|
|
| Basic type classes
|
|
|
The Eq class defines equality (==) and inequality (/=).
All the basic datatypes exported by the Prelude are instances of Eq,
and Eq may be derived for any datatype whose constituents are also
instances of Eq.
Minimal complete definition: either == or /=.
| | | Methods | | | Instances | | Eq All | | Eq Any | | Eq ArithException | | Eq ArrayException | | Eq AsyncException | | Eq Bool | | Eq BufferMode | | Eq BufferState | | Eq CCc | | Eq CChar | | Eq CClock | | Eq CDev | | Eq CDouble | | Eq CFloat | | Eq CGid | | Eq CIno | | Eq CInt | | Eq CIntMax | | Eq CIntPtr | | Eq CLDouble | | Eq CLLong | | Eq CLong | | Eq CMode | | Eq CNlink | | Eq COff | | Eq CPid | | Eq CPtrdiff | | Eq CRLim | | Eq CSChar | | Eq CShort | | Eq CSigAtomic | | Eq CSize | | Eq CSpeed | | Eq CSsize | | Eq CTcflag | | Eq CTime | | Eq CUChar | | Eq CUInt | | Eq CUIntMax | | Eq CUIntPtr | | Eq CULLong | | Eq CULong | | Eq CUShort | | Eq CUid | | Eq CWchar | | Eq Char | | Eq Constr | | Eq ConstrRep | | Eq DataRep | | Eq Double | | Eq Errno | | Eq Exception | | Eq ExitCode | | Eq FDType | | Eq Fd | | Eq Fixity | | Eq Float | | Eq GeneralCategory | | Eq Handle | | Eq HandlePosn | | Eq HashData | | Eq IOErrorType | | Eq IOException | | Eq IOMode | | Eq Inserts | | Eq Int | | Eq Int16 | | Eq Int32 | | Eq Int64 | | Eq Int8 | | Eq IntPtr | | Eq Integer | | Eq Key | | Eq KeyPr | | Eq Lexeme | | Eq Ordering | | Eq SeekMode | | Eq ThreadId | | Eq Timeout | | Eq TyCon | | Eq TypeRep | | Eq Unique | | Eq Version | | Eq Word | | Eq Word16 | | Eq Word32 | | Eq Word64 | | Eq Word8 | | Eq WordPtr | | Eq () | | (Eq a, Eq b) => Eq (a, b) | | (Eq a, Eq b, Eq c) => Eq (a, b, c) | | (Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d) | | (Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (a, b, c, d, e) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f) => Eq (a, b, c, d, e, f) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g) => Eq (a, b, c, d, e, f, g) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h) => Eq (a, b, c, d, e, f, g, h) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i) => Eq (a, b, c, d, e, f, g, h, i) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j) => Eq (a, b, c, d, e, f, g, h, i, j) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k) => Eq (a, b, c, d, e, f, g, h, i, j, k) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l) => Eq (a, b, c, d, e, f, g, h, i, j, k, l) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n, Eq o) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | | (RealFloat a, Eq a) => Eq (Complex a) | | Eq a => Eq (Dual a) | | Eq a => Eq (First a) | | Eq (Fixed a) | | Eq (ForeignPtr a) | | Eq (FunPtr a) | | Eq (IORef a) | | Eq a => Eq (Last a) | | Eq (MVar a) | | Eq a => Eq (Maybe a) | | Eq a => Eq (Product a) | | Eq (Ptr a) | | (Integral a, Eq a) => Eq (Ratio a) | | Eq (StableName a) | | Eq (StablePtr a) | | Eq a => Eq (Sum a) | | Eq (TVar a) | | Eq a => Eq [a] | | (Ix i, Eq e) => Eq (Array i e) | | (Eq a, Eq b) => Eq (Either a b) | | Eq (IOArray i e) | | Eq (STRef s a) | | Eq (STArray s i e) |
|
|
|
|
The Ord class is used for totally ordered datatypes.
Instances of Ord can be derived for any user-defined
datatype whose constituent types are in Ord. The declared order
of the constructors in the data declaration determines the ordering
in derived Ord instances. The Ordering datatype allows a single
comparison to determine the precise ordering of two objects.
Minimal complete definition: either compare or <=.
Using compare can be more efficient for complex types.
| | | Methods | | | Instances | | Ord All | | Ord Any | | Ord ArithException | | Ord ArrayException | | Ord AsyncException | | Ord Bool | | Ord BufferMode | | Ord CCc | | Ord CChar | | Ord CClock | | Ord CDev | | Ord CDouble | | Ord CFloat | | Ord CGid | | Ord CIno | | Ord CInt | | Ord CIntMax | | Ord CIntPtr | | Ord CLDouble | | Ord CLLong | | Ord CLong | | Ord CMode | | Ord CNlink | | Ord COff | | Ord CPid | | Ord CPtrdiff | | Ord CRLim | | Ord CSChar | | Ord CShort | | Ord CSigAtomic | | Ord CSize | | Ord CSpeed | | Ord CSsize | | Ord CTcflag | | Ord CTime | | Ord CUChar | | Ord CUInt | | Ord CUIntMax | | Ord CUIntPtr | | Ord CULLong | | Ord CULong | | Ord CUShort | | Ord CUid | | Ord CWchar | | Ord Char | | Ord Double | | Ord ExitCode | | Ord Fd | | Ord Float | | Ord GeneralCategory | | Ord IOMode | | Ord Int | | Ord Int16 | | Ord Int32 | | Ord Int64 | | Ord Int8 | | Ord IntPtr | | Ord Integer | | Ord Ordering | | Ord SeekMode | | Ord ThreadId | | Ord Unique | | Ord Version | | Ord Word | | Ord Word16 | | Ord Word32 | | Ord Word64 | | Ord Word8 | | Ord WordPtr | | Ord () | | (Ord a, Ord b) => Ord (a, b) | | (Ord a, Ord b, Ord c) => Ord (a, b, c) | | (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d) | | (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) => Ord (a, b, c, d, e, f) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) => Ord (a, b, c, d, e, f, g) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h) => Ord (a, b, c, d, e, f, g, h) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i) => Ord (a, b, c, d, e, f, g, h, i) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j) => Ord (a, b, c, d, e, f, g, h, i, j) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j, Ord k) => Ord (a, b, c, d, e, f, g, h, i, j, k) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j, Ord k, Ord l) => Ord (a, b, c, d, e, f, g, h, i, j, k, l) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j, Ord k, Ord l, Ord m) => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n) => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | | (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i, Ord j, Ord k, Ord l, Ord m, Ord n, Ord o) => Ord (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | | Ord a => Ord (Dual a) | | Ord a => Ord (First a) | | Ord (Fixed a) | | Ord (ForeignPtr a) | | Ord (FunPtr a) | | Ord a => Ord (Last a) | | Ord a => Ord (Maybe a) | | Ord a => Ord (Product a) | | Ord (Ptr a) | | Integral a => Ord (Ratio a) | | Ord a => Ord (Sum a) | | Ord a => Ord [a] | | (Ix i, Ord e) => Ord (Array i e) | | (Ord a, Ord b) => Ord (Either a b) |
|
|
|
|
Class Enum defines operations on sequentially ordered types.
The enumFrom... methods are used in Haskell's translation of
arithmetic sequences.
Instances of Enum may be derived for any enumeration type (types
whose constructors have no fields). The nullary constructors are
assumed to be numbered left-to-right by fromEnum from 0 through n-1.
See Chapter 10 of the Haskell Report for more details.
For any type that is an instance of class Bounded as well as Enum,
the following should hold:
enumFrom x = enumFromTo x maxBound
enumFromThen x y = enumFromThenTo x y bound
where
bound | fromEnum y >= fromEnum x = maxBound
| otherwise = minBound
| | | Methods | | | the successor of a value. For numeric types, succ adds 1.
| | | | the predecessor of a value. For numeric types, pred subtracts 1.
| | | | Convert from an Int.
| | | | Convert to an Int.
It is implementation-dependent what fromEnum returns when
applied to a value that is too large to fit in an Int.
| | | | Used in Haskell's translation of [n..].
| | | enumFromThen :: a -> a -> [a] | Source |
| | Used in Haskell's translation of [n,n'..].
| | | enumFromTo :: a -> a -> [a] | Source |
| | Used in Haskell's translation of [n..m].
| | | enumFromThenTo :: a -> a -> a -> [a] | Source |
| | Used in Haskell's translation of [n,n'..m].
|
| | Instances | |
|
|
|
The Bounded class is used to name the upper and lower limits of a
type. Ord is not a superclass of Bounded since types that are not
totally ordered may also have upper and lower bounds.
The Bounded class may be derived for any enumeration type;
minBound is the first constructor listed in the data declaration
and maxBound is the last.
Bounded may also be derived for single-constructor datatypes whose
constituent types are in Bounded.
| | | Methods | | | Instances | |
|