| Portability | portable |
|---|---|
| Stability | stable |
| Maintainer | libraries@haskell.org |
Prelude
Contents
Description
The Prelude: a standard module imported by default into all Haskell modules. For more documentation, see the Haskell 98 Reporthttp://www.haskell.org/onlinereport/.
Synopsis
TheMaybe type encapsulates an optional value. A value of type either contains a value of typeMaybe aa (represented as), or it is empty (represented asJust aNothing). UsingMaybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such aserror.
TheMaybe type is also a monad. It is a simple kind of error monad, where all errors are represented byNothing. A richer error monad can be built using theData.Either.Either type.
Instances
| MonadMaybe | |
| FunctorMaybe | |
| Typeable1Maybe | |
| MonadFixMaybe | |
| MonadPlusMaybe | |
| ApplicativeMaybe | |
| FoldableMaybe | |
| TraversableMaybe | |
| AlternativeMaybe | |
| Eq a =>Eq (Maybe a) | |
| Data a =>Data (Maybe a) | |
| Ord a =>Ord (Maybe a) | |
| Read a =>Read (Maybe a) | |
| Show a =>Show (Maybe a) | |
| Generic (Maybe a) | |
| Monoid a =>Monoid (Maybe a) | Lift a semigroup into |
TheEither type represents values with two possibilities: a value oftype is eitherEither a b orLeft a.Right b
TheEither type is sometimes used to represent a value which iseither correct or an error; by convention, theLeft constructor isused to hold an error value and theRight constructor is used tohold a correct value (mnemonic: "right" also means "correct").
The character typeChar is an enumeration whose values representUnicode (or equivalently ISO/IEC 10646) characters(seehttp://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 ASCIIcharacter set (the first 128 characters).A character literal in Haskell has typeChar.
To convert aChar to or from the correspondingInt value definedby Unicode, usePrelude.toEnum andPrelude.fromEnum from thePrelude.Enum class respectively (or equivalentlyord andchr).
curry :: ((a, b) -> c) -> a -> b -> cSource
curry converts an uncurried function to a curried function.
uncurry :: (a -> b -> c) -> (a, b) -> cSource
uncurry converts a curried function to a function on pairs.
TheEq class defines equality (==) and inequality (/=). All the basic datatypes exported by thePrelude are instances ofEq, andEq may be derived for any datatype whose constituents are also instances ofEq.
Instances
TheOrd class is used for totally ordered datatypes.
Instances ofOrd can be derived for any user-defined datatype whose constituent types are inOrd. The declared order of the constructors in the data declaration determines the ordering in derivedOrd instances. TheOrdering datatype allows a single comparison to determine the precise ordering of two objects.
Minimal complete definition: eithercompare or<=. Usingcompare can be more efficient for complex types.
Instances
ClassEnum defines operations on sequentially ordered types.
TheenumFrom... methods are used in Haskell's translation of arithmetic sequences.
Instances ofEnum may be derived for any enumeration type (types whose constructors have no fields). The nullary constructors are assumed to be numbered left-to-right byfromEnum from0 throughn-1. See Chapter 10 of theHaskell Report for more details.
For any type that is an instance of classBounded as well asEnum, the following should hold:
succmaxBound andpredminBound should result in a runtime error.fromEnum andtoEnum should give a runtime error if the result value is not representable in the result type. For example,toEnum 7 ::Bool is an error.enumFrom andenumFromThen should be defined with an implicit bound, thus: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 anInt.
Convert to anInt. It is implementation-dependent whatfromEnum returns when applied to a value that is too large to fit in anInt.
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
TheBounded class is used to name the upper and lower limits of a type.Ord is not a superclass ofBounded since types that are not totally ordered may also have upper and lower bounds.
TheBounded class may be derived for any enumeration type;minBound is the first constructor listed in thedata declaration andmaxBound is the last.Bounded may also be derived for single-constructor datatypes whose constituent types are inBounded.
Instances
A fixed-precision integer type with at least the range[-2^29 .. 2^29-1]. The exact range for a given implementation can be determined by usingPrelude.minBound andPrelude.maxBound from thePrelude.Bounded class.
Arbitrary-precision integers.
Single-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE single-precision type.
Double-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE double-precision type.
class (Eq a,Show a) =>Num awhereSource
Basic numeric class.
Minimal complete definition: all exceptnegate or(-)
Methods
(+),(*),(-) :: a -> a -> aSource
Unary negation.
Absolute value.
Sign of a number. The functionsabs andsignum should satisfy the law:
abs x * signum x == x
For real numbers, thesignum is either-1 (negative),0 (zero) or1 (positive).
fromInteger ::Integer -> aSource
Conversion from anInteger. An integer literal represents the application of the functionfromInteger to the appropriate value of typeInteger, so such literals have type(.Num a) => a
Instances
class (Num a,Ord a) =>Real awhereSource
Methods
toRational :: a ->RationalSource
the rational equivalent of its real argument with full precision
Instances
class (Real a,Enum a) =>Integral awhereSource
Methods
integer division truncated toward zero
integer remainder, satisfying
(x `quot` y)*y + (x `rem` y) == x
integer division truncated toward negative infinity
integer modulus, satisfying
(x `div` y)*y + (x `mod` y) == x
quotRem :: a -> a -> (a, a)Source
divMod :: a -> a -> (a, a)Source
toInteger :: a ->IntegerSource
conversion toInteger
Instances
classNum a =>Fractional awhereSource
Fractional numbers, supporting real division.
Minimal complete definition:fromRational and (recip or()/)
Methods
fractional division
reciprocal fraction
fromRational ::Rational -> aSource
Conversion from aRational (that is). A floating literal stands for an application ofRatioIntegerfromRational to a value of typeRational, so such literals have type(.Fractional a) => a
Instances
| FractionalDouble | |
| FractionalFloat | |
| FractionalCDouble | |
| FractionalCFloat | |
| Integral a =>Fractional (Ratio a) | |
| RealFloat a =>Fractional (Complex a) | |
| HasResolution a =>Fractional (Fixed a) |
classFractional a =>Floating awhereSource
Trigonometric and hyperbolic functions and related functions.
Minimal complete definition:pi,exp,log,sin,cos,sinh,cosh,asin,acos,atan,asinh,acosh andatanh
class (Real a,Fractional a) =>RealFrac awhereSource
Extracting components of fractions.
Minimal complete definition:properFraction
Methods
properFraction ::Integral b => a -> (b, a)Source
The functionproperFraction takes a real fractional numberx and returns a pair(n,f) such thatx = n+f, and:
n is an integral number with the same sign asx; andf is a fraction with the same type and sign asx, and with absolute value less than1.The default definitions of theceiling,floor,truncate andround functions are in terms ofproperFraction.
truncate ::Integral b => a -> bSource
returns the integer nearesttruncate xx between zero andx
round ::Integral b => a -> bSource
returns the nearest integer toround xx; the even integer ifx is equidistant between two integers
ceiling ::Integral b => a -> bSource
returns the least integer not less thanceiling xx
floor ::Integral b => a -> bSource
returns the greatest integer not greater thanfloor xx
class (RealFrac a,Floating a) =>RealFloat awhereSource
Efficient, machine-independent access to the components of a floating-point number.
Minimal complete definition: all exceptexponent,significand,scaleFloat andatan2
Methods
floatRadix :: a ->IntegerSource
a constant function, returning the radix of the representation (often2)
floatDigits :: a ->IntSource
a constant function, returning the number of digits offloatRadix in the significand
floatRange :: a -> (Int,Int)Source
a constant function, returning the lowest and highest values the exponent may assume
decodeFloat :: a -> (Integer,Int)Source
The functiondecodeFloat applied to a real floating-point number returns the significand expressed as anInteger and an appropriately scaled exponent (anInt). If yieldsdecodeFloat x(m,n), thenx is equal in value tom*b^^n, whereb is the floating-point radix, and furthermore, eitherm andn are both zero or elseb^(d-1) <= m < b^d, whered is the value of. In particular,floatDigits x.decodeFloat 0 = (0,0)
encodeFloat ::Integer ->Int -> aSource
encodeFloat performs the inverse ofdecodeFloat
the second component ofdecodeFloat.
significand :: a -> aSource
the first component ofdecodeFloat, scaled to lie in the open interval (-1,1)
scaleFloat ::Int -> a -> aSource
multiplies a floating-point number by an integer power of the radix
True if the argument is an IEEE "not-a-number" (NaN) value
isInfinite :: a ->BoolSource
True if the argument is an IEEE infinity or negative infinity
isDenormalized :: a ->BoolSource
True if the argument is too small to be represented in normalized format
isNegativeZero :: a ->BoolSource
True if the argument is an IEEE negative zero
True if the argument is an IEEE floating point number
a version of arctangent taking two real floating-point arguments. For real floatingx andy, computes the angle (from the positive x-axis) of the vector from the origin to the pointatan2 y x(x,y). returns a value in the range [atan2 y x-pi,pi]. It follows the Common Lisp semantics for the origin when signed zeroes are supported., withatan2 y 1y in a type that isRealFloat, should return the same value as. A default definition ofatan yatan2 is provided, but implementors can provide a more accurate implementation.
gcd ::Integral a => a -> a -> aSource
is the non-negative factor of bothgcd x yx andy of which every common factor ofx andy is also a factor; for example,gcd 4 2 = 2,gcd (-4) 6 = 2 =gcd 0 44. =gcd 0 00. (That is, the common divisor that is "greatest" in the divisibility preordering.)
Note: Since for signed fixed-width integer types,, the result may be negative if one of the arguments isabsminBound < 0 (and necessarily is if the other isminBound0 or) for such types.minBound
lcm ::Integral a => a -> a -> aSource
is the smallest positive integer that bothlcm x yx andy divide.
(^^) :: (Fractional a,Integral b) => a -> b -> aSource
raise a number to an integral power
fromIntegral :: (Integral a,Num b) => a -> bSource
general coercion from integral types
realToFrac :: (Real a,Fractional b) => a -> bSource
general coercion to fractional types
TheMonad class defines the basic operations over amonad,a concept from a branch of mathematics known ascategory theory.From the perspective of a Haskell programmer, however, it is best tothink of a monad as anabstract datatype of actions.Haskell'sdo expressions provide a convenient syntax for writingmonadic expressions.
Minimal complete definition:>>= andreturn.
Instances ofMonad should satisfy the following laws:
return a >>= k == k a m >>= return == m m >>= (\x -> k x >>= h) == (m >>= k) >>= h
Instances of bothMonad andFunctor should additionally satisfy the law:
fmap f xs == xs >>= return . f
The instances ofMonad for lists,Data.Maybe.Maybe andSystem.IO.IOdefined in thePrelude satisfy these laws.
Methods
(>>=) ::forall a b. m a -> (a -> m b) -> m bSource
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
(>>) ::forall a b. m a -> m b -> m bSource
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
Inject a value into the monadic type.
Fail with a message. This operation is not part of the mathematical definition of a monad, but is invoked on pattern-match failure in ado expression.
TheFunctor class is used for types that can be mapped over.Instances ofFunctor should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances ofFunctor for lists,Data.Maybe.Maybe andSystem.IO.IOsatisfy these laws.
Instances
| Functor [] | |
| FunctorIO | |
| FunctorMaybe | |
| FunctorReadP | |
| FunctorReadPrec | |
| FunctorSTM | |
| FunctorZipList | |
| Functor Id | |
| Functor ((->) r) | |
| Functor (Either a) | |
| Functor ((,) a) | |
| Functor (ST s) | |
| Ix i =>Functor (Array i) | |
| Functor (ST s) | |
| Monad m =>Functor (WrappedMonad m) | |
| Functor (Const m) | |
| Functor (StateR s) | |
| Functor (StateL s) | |
| Arrow a =>Functor (WrappedArrow a b) |
sequence ::Monad m => [m a] -> m [a]Source
Evaluate each action in the sequence from left to right, and collect the results.
sequence_ ::Monad m => [m a] -> m()Source
Evaluate each action in the sequence from left to right, and ignore the results.
flip :: (a -> b -> c) -> b -> a -> cSource
takes its (first) two arguments in the reverse order offlip ff.
($) :: (a -> b) -> a -> bSource
Application operator. This operator is redundant, since ordinary application(f x) means the same as(f. However,$ x)$ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted; for example:
f $ g $ h x = f (g (h x))
It is also useful in higher-order situations, such as, ormap ($ 0) xs.Data.List.zipWith ($) fs xs
until :: (a ->Bool) -> (a -> a) -> a -> aSource
yields the result of applyinguntil p ff untilp holds.
Evaluates its first argument to head normal form, and then returns its second argument as the result.
map :: (a -> b) -> [a] -> [b]Source
mapf xs is the list obtained by applyingf to each element ofxs, i.e.,
map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] map f [x1, x2, ...] == [f x1, f x2, ...]
(++) :: [a] -> [a] -> [a]Source
Append two lists, i.e.,
[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn] [x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]
If the first list is not finite, the result is the first list.
filter :: (a ->Bool) -> [a] -> [a]Source
filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,
filter p xs = [ x | x <- xs, p x]
Return all the elements of a list except the last one. The list must be non-empty.
List index (subscript) operator, starting from 0. It is an instance of the more generalData.List.genericIndex, which takes an index of any integral type.
foldl :: (a -> b -> a) -> a -> [b] -> aSource
foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
The list must be finite.
foldr :: (a -> b -> b) -> b -> [a] -> bSource
foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
product ::Num a => [a] -> aSource
Theproduct function computes the product of a finite list of numbers.
iterate :: (a -> a) -> a -> [a]Source
iteratef x returns an infinite list of repeated applications off tox:
iterate f x == [x, f x, f (f x), ...]
replicate ::Int -> a -> [a]Source
replicaten x is a list of lengthn withx the value of every element. It is an instance of the more generalData.List.genericReplicate, in whichn may be of any integral type.
cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.
take ::Int -> [a] -> [a]Source
taken, applied to a listxs, returns the prefix ofxs of lengthn, orxs itself ifn >:length xs
take 5 "Hello World!" == "Hello" take 3 [1,2,3,4,5] == [1,2,3] take 3 [1,2] == [1,2] take 3 [] == [] take (-1) [1,2] == [] take 0 [1,2] == []
It is an instance of the more generalData.List.genericTake, in whichn may be of any integral type.
drop ::Int -> [a] -> [a]Source
dropn xs returns the suffix ofxs after the firstn elements, or[] ifn >:length xs
drop 6 "Hello World!" == "World!" drop 3 [1,2,3,4,5] == [4,5] drop 3 [1,2] == [] drop 3 [] == [] drop (-1) [1,2] == [1,2] drop 0 [1,2] == [1,2]
It is an instance of the more generalData.List.genericDrop, in whichn may be of any integral type.
splitAt ::Int -> [a] -> ([a], [a])Source
splitAtn xs returns a tuple where first element isxs prefix of lengthn and second element is the remainder of the list:
splitAt 6 "Hello World!" == ("Hello ","World!") splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5]) splitAt 1 [1,2,3] == ([1],[2,3]) splitAt 3 [1,2,3] == ([1,2,3],[]) splitAt 4 [1,2,3] == ([1,2,3],[]) splitAt 0 [1,2,3] == ([],[1,2,3]) splitAt (-1) [1,2,3] == ([],[1,2,3])It is equivalent to( whentake n xs,drop n xs)n is not_|_ (splitAt _|_ xs = _|_).splitAt is an instance of the more generalData.List.genericSplitAt, in whichn may be of any integral type.
takeWhile :: (a ->Bool) -> [a] -> [a]Source
takeWhile, applied to a predicatep and a listxs, returns the longest prefix (possibly empty) ofxs of elements that satisfyp:
takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2] takeWhile (< 9) [1,2,3] == [1,2,3] takeWhile (< 0) [1,2,3] == []
span :: (a ->Bool) -> [a] -> ([a], [a])Source
span, applied to a predicatep and a listxs, returns a tuple where first element is longest prefix (possibly empty) ofxs of elements that satisfyp and second element is the remainder of the list:
span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4]) span (< 9) [1,2,3] == ([1,2,3],[]) span (< 0) [1,2,3] == ([],[1,2,3])
break :: (a ->Bool) -> [a] -> ([a], [a])Source
break, applied to a predicatep and a listxs, returns a tuple where first element is longest prefix (possibly empty) ofxs of elements thatdo not satisfyp and second element is the remainder of the list:
break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4]) break (< 9) [1,2,3] == ([],[1,2,3]) break (> 9) [1,2,3] == ([1,2,3],[])
lookup ::Eq a => a -> [(a, b)] ->Maybe bSource
lookupkey assocs looks up a key in an association list.
zip :: [a] -> [b] -> [(a, b)]Source
zip takes two lists and returns a list of corresponding pairs. If one input list is short, excess elements of the longer list are discarded.
unzip :: [(a, b)] -> ([a], [b])Source
unzip transforms a list of pairs into a list of first components and a list of second components.
lines ::String -> [String]Source
lines breaks a string up into a list of strings at newline characters. The resulting strings do not contain newlines.
words ::String -> [String]Source
words breaks a string up into a list of words, which were delimited by white space.
StringStringConversion of values to readableStrings.
Minimal complete definition:showsPrec orshow.
Derived instances ofShow have the following properties, which are compatible with derived instances ofText.Read.Read:
show is a syntactically correct Haskell expression containing only constants, given the fixity declarations in force at the point where the type is declared. It contains only the constructor names defined in the data type, parentheses, and spaces. When labelled constructor fields are used, braces, commas, field names, and equal signs are also used.showsPrec will produce infix applications of the constructor.x is less thand (associativity is ignored). Thus, ifd is0 then the result is never surrounded in parentheses; ifd is11 it is always surrounded in parentheses, unless it is an atomic expression.show will produce the record-syntax form, with the fields given in the same order as the original declaration.For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance ofShow is equivalent to
instance (Show a) => Show (Tree a) where showsPrec d (Leaf m) = showParen (d > app_prec) $ showString "Leaf " . showsPrec (app_prec+1) m where app_prec = 10 showsPrec d (u :^: v) = showParen (d > up_prec) $ showsPrec (up_prec+1) u . showString " :^: " . showsPrec (up_prec+1) v where up_prec = 5
Note that right-associativity of:^: is ignored. For example,
show (Leaf 1 :^: Leaf 2 :^: Leaf 3) produces the string"Leaf 1 :^: (Leaf 2 :^: Leaf 3)".Methods
Arguments
| ::Int | the operator precedence of the enclosing context (a number from |
| -> a | the value to be converted to a |
| ->ShowS |
Convert a value to a readableString.
showsPrec should satisfy the law
showsPrec d x r ++ s == showsPrec d x (r ++ s)
Derived instances ofText.Read.Read andShow satisfy the following:
(x,"") is an element of(Text.Read.readsPrec d (showsPrec d x "")).That is,Text.Read.readsPrec parses the string produced byshowsPrec, and delivers the value thatshowsPrec started with.
Instances
utility function converting aChar to a show function that simply prepends the character unchanged.
showString ::String ->ShowSSource
utility function converting aString to a show function that simply prepends the string unchanged.
StringParsing ofStrings, producing values.
Minimal complete definition:readsPrec (or, for GHC only,readPrec)
Derived instances ofRead make the following assumptions, which derived instances ofText.Show.Show obey:
Read instance will parse only infix applications of the constructor (not the prefix form).Read will parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration.Read instance allows arbitrary Haskell whitespace between tokens of the input string. Extra parentheses are also allowed.For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance ofRead in Haskell 98 is equivalent to
instance (Read a) => Read (Tree a) where readsPrec d r = readParen (d > app_prec) (\r -> [(Leaf m,t) | ("Leaf",s) <- lex r, (m,t) <- readsPrec (app_prec+1) s]) r ++ readParen (d > up_prec) (\r -> [(u:^:v,w) | (u,s) <- readsPrec (up_prec+1) r, (":^:",t) <- lex s, (v,w) <- readsPrec (up_prec+1) t]) r where app_prec = 10 up_prec = 5Note that right-associativity of:^: is unused.
The derived instance in GHC is equivalent to
instance (Read a) => Read (Tree a) where readPrec = parens $ (prec app_prec $ do Ident "Leaf" <- lexP m <- step readPrec return (Leaf m)) +++ (prec up_prec $ do u <- step readPrec Symbol ":^:" <- lexP v <- step readPrec return (u :^: v)) where app_prec = 10 up_prec = 5 readListPrec = readListPrecDefault
Methods
Arguments
| ::Int | the operator precedence of the enclosing context (a number from |
| ->ReadS a |
attempts to parse a value from the front of the string, returning a list of (parsed value, remaining string) pairs. If there is no successful parse, the returned list is empty.
Derived instances ofRead andText.Show.Show satisfy the following:
(x,"") is an element of(readsPrec d (Text.Show.showsPrec d x "")).That is,readsPrec parses the string produced byText.Show.showsPrec, and delivers the value thatText.Show.showsPrec started with.
Instances
read ::Read a =>String -> aSource
Theread function reads input from a string, which must be completely consumed by the input process.
Thelex function reads a single lexeme from the input, discarding initial white space, and returning the characters that constitute the lexeme. If the input string contains only white space,lex returns a single successful `lexeme' consisting of the empty string. (Thus.) If there is no legal lexeme at the beginning of the input string,lex "" = [("","")]lex fails (i.e. returns[]).
This lexer is not completely faithful to the Haskell lexical syntax in the following respects:
A value of type is a computation which, when performed,does some I/O before returning a value of typeIO aa.
There is really only one way to "perform" an I/O action: bind it toMain.main in your program. When your program is run, the I/O willbe performed. It isn't possible to perform I/O from an arbitraryfunction, unless that function is itself in theIO monad and calledat some point, directly or indirectly, fromMain.main.
IO is a monad, soIO actions can be combined using either the do-notationor the>> and>>= operations from theMonad class.
Instances
print ::Show a => a ->IO()Source
Theprint function outputs a value of any printable type to the standard output device. Printable types are those that are instances of classShow;print converts values to strings for output using theshow operation and adds a newline.
For example, a program to print the first 20 integers and their powers of 2 could be written as:
main = print ([(n, 2^n) | n <- [0..19]])
ThegetContents operation returns all user input as a single string, which is read lazily as it is needed (same ashGetContentsstdin).
interact :: (String ->String) ->IO()Source
Theinteract function takes a function of typeString->String as its argument. The entire input from the standard input device is passed to this function as its argument, and the resulting string is output on the standard output device.
File and directory names are values of typeString, whose precise meaning is operating system dependent. Files can be opened, yielding a handle which can then be used to operate on the contents of that file.
readFile ::FilePath ->IOStringSource
ThereadFile function reads a file and returns the contents of the file as a string. The file is read lazily, on demand, as withgetContents.
writeFile ::FilePath ->String ->IO()Source
The computationwriteFilefile str function writes the stringstr, to the filefile.
appendFile ::FilePath ->String ->IO()Source
The computationappendFilefile str function appends the stringstr, to the filefile.
Note thatwriteFile andappendFile write a literal string to a file. To write a value of any printable type, as withprint, use theshow function to convert the value to a string first.
main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
typeIOError =IOExceptionSource
The Haskell 98 type for exceptions in theIO monad. Any I/O operation may raise anIOError instead of returning a result. For a more general type of exception, including also those that arise in pure code, seeControl.Exception.Exception.
In Haskell 98, this is an opaque type.
Produced byHaddock version 2.9.2