-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Common not-so-common functions for lists -- -- Common not-so-common functions for lists. -- -- Since Data.List.Extras is prime realestate for extensions to -- Data.List, if you have something you'd like to contribute feel -- free to contact the maintainer (I'm friendly). I'm amenable to -- adopting code if you think your functions aren't enough for a package -- on their own. Or if you would rather maintain a separate package I can -- share the Data.List.Extras.Foo namespace. @package list-extras @version 0.2.2 -- | This module provides variants of the maximum and minimum -- functions which return the elements for which some function is -- maximized or minimized. module Data.List.Extras.Argmax -- | Apply a list function safely, i.e. when the list is non-empty. All -- other functions will throw errors on empty lists, so use this to make -- your own safe variations. catchNull :: ([a] -> b) -> ([a] -> Maybe b) -- | Return an element of the list which maximizes the function according -- to a user-defined ordering. argmaxBy :: (b -> b -> Ordering) -> (a -> b) -> [a] -> a -- | Return all elements of the list which maximize the function according -- to a user-defined ordering. argmaxesBy :: (b -> b -> Ordering) -> (a -> b) -> [a] -> [a] -- | Return an element of the list which maximizes the function according -- to a user-defined ordering, and return the value of the function at -- that element as well. argmaxWithMaxBy :: (b -> b -> Ordering) -> (a -> b) -> [a] -> (a, b) -- | Return all elements of the list which maximize the function according -- to a user-defined ordering, and return the value of the function at -- those elements as well. argmaxesWithMaxBy :: (b -> b -> Ordering) -> (a -> b) -> [a] -> ([a], b) -- | Return an element of the list which maximizes the function. argmax :: (Ord b) => (a -> b) -> [a] -> a -- | Return all elements of the list which maximize the function. argmaxes :: (Ord b) => (a -> b) -> [a] -> [a] -- | Return an element of the list which maximizes the function, and return -- the value of the function at that element as well. argmaxWithMax :: (Ord b) => (a -> b) -> [a] -> (a, b) -- | Return all elements of the list which maximize the function, and -- return the value of the function at those elements as well. argmaxesWithMax :: (Ord b) => (a -> b) -> [a] -> ([a], b) -- | Return an element of the list which minimizes the function. argmin :: (Ord b) => (a -> b) -> [a] -> a -- | Return all elements of the list which minimize the function. argmins :: (Ord b) => (a -> b) -> [a] -> [a] -- | Return an element of the list which minimizes the function, and return -- the value of the function at that element as well. argminWithMin :: (Ord b) => (a -> b) -> [a] -> (a, b) -- | Return all elements of the list which minimize the function, and -- return the value of the function at those elements as well. argminsWithMin :: (Ord b) => (a -> b) -> [a] -> ([a], b) -- | This module provides safe zipping functions which will fail (return -- Nothing) on uneven length lists. module Data.List.Extras.Pair -- | A generic version of pair. The first argument is a tuple -- homomorphism (i.e. a function for how to combine values from the two -- lists), the second two arguments form a list homomorphism (i.e. so you -- can foldr the [c] list directly without actually -- constructing it). -- -- In order to evaluate to WHNF pairWithBy is strict in both list -- arguments, as it must be, to determine that the lists are of the same -- length. This means it can survive one infinite list (yielding -- Nothing) but that it can't survive two. The implementation is -- very efficient and uses a tight tail-recursive loop, however with -- extremely long lists it will be churning through heap and that -- tightness can make it hard to interrupt (lists of 1 million elements -- return in 1~2 seconds, but lists of 10 million can lock your system -- up). pairWithBy :: (a -> b -> c) -> (c -> d -> d) -> d -> [a] -> [b] -> Maybe d -- | A safe version of zipWith. pairWith :: (a -> b -> c) -> [a] -> [b] -> Maybe [c] -- | A safe version of zip that uses a user-defined list -- homomorphism. pairBy :: ((a, b) -> m (a, b) -> m (a, b)) -> m (a, b) -> [a] -> [b] -> Maybe (m (a, b)) -- | A safe version of zip. pair :: [a] -> [b] -> Maybe [(a, b)] -- | A bijection from a list of functions and a list of arguments to a list -- of results of applying the functions bijectively. biject :: [a -> b] -> [a] -> Maybe [b] -- | A version of biject that applies functions strictly. N.B. the -- list is still lazily evaluated, this just makes the functions strict -- in their argument. biject' :: [a -> b] -> [a] -> Maybe [b] -- | An unsafe variant of pairWithBy to fill out the interface. zipWithBy :: (a -> b -> c) -> (c -> d -> d) -> d -> [a] -> [b] -> d -- | A version of zip that uses a user-defined list homomorphism. zipBy :: ((a, b) -> m (a, b) -> m (a, b)) -> m (a, b) -> [a] -> [b] -> m (a, b) -- | This module provides least-strict functions for getting a list's -- length and doing natural things with it. On GHC this module also uses -- rewrite rules to convert certain calls to length into our -- least-strict versions. -- -- The regular version of length will traverse the entire spine -- of the list in order to return an answer. For comparing the length -- against some bound, that is by far too strict. Being too strict can -- cause a space leak by expanding a lazy list before necessary (or more -- than is ever necessary). And it can lead to unnecessarily -- non-terminating programs when trying to determine if an infinite list -- is longer or shorter than some finite bound. -- -- A nicer version of length would return some lazy -- approximation of an answer which retains the proper semantics. An -- option for doing this is to return Peano integers which can be -- decremented as much as necessary and no further (i.e. at most one more -- than the bound). Of course, Peano integers are woefully inefficient -- and would wreck the cache and burn heap. This module provides -- functions with the same lazy effect as if we used Peano integers, but -- does so efficiently instead. -- -- (For Peano integers see numbers:Data.Number.Natural or -- non-negative:Numeric.NonNegative.Class.) module Data.List.Extras.LazyLength -- | A variant of length which is least-strict for comparing against -- a boundary length. This function is defined primarily for use by -- rewrite rules rather than for direct use (though it's fine for that -- too). -- -- lengthBound is polymorphic in the return of the helper -- function so we can use compare as well as >, -- >=, ==, /=, <=, <. If you -- want to use any other functions, know that we only preserve the -- ordering of the list's length vs the boundary length and so the -- function should not rely on the true values of either of the numbers -- being compared. lengthBound :: Int -> (Int -> Int -> a) -> [b] -> a -- | A variant of length which is least-strict for comparing the -- lengths of two lists. This is as strict as the length of the shorter -- list (which allows comparing an infinite list against a finite list). -- The function itself is trivial, but again it's designed primarily for -- rewrite rules. -- -- If you're going to immediately follow this with a zip function -- then see Data.List.Extras.Pair instead. lengthCompare :: [a] -> [b] -> Ordering -- | This module provides a single header for all -- Data.List.Extras.* modules module Data.List.Extras -- | This module provides the Prelude but removing all the list -- functions. This is helpful for modules that overload those function -- names to work for other types. -- -- Be sure to disable the implicit importing of the prelude when you -- import this one (by passing -fno-implicit-prelude for GHC, or -- by explicitly importing the prelude with an empty import list for most -- implementations). module Prelude.Listless -- | Strict (call-by-value) application, defined in terms of seq. ($!) :: (a -> b) -> a -> b -- | Application operator. This operator is redundant, since ordinary -- application (f x) means the same as (f $ x). -- However, $ 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 map -- ($ 0) xs, or Data.List.zipWith ($) fs xs. ($) :: (a -> b) -> a -> b -- | Boolean "and" (&&) :: Bool -> Bool -> Bool -- | Function composition. (.) :: (b -> c) -> (a -> b) -> a -> c -- | Same as >>=, but with the arguments interchanged. (=<<) :: (Monad m) => (a -> m b) -> m a -> m b data Bool :: * False :: Bool True :: Bool -- | 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. class Bounded a minBound :: (Bounded a) => a maxBound :: (Bounded a) => a data Char :: * -- | 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. data Double :: * -- | 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"). data Either a b :: * -> * -> * Left :: a -> Either a b Right :: b -> 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 class Enum a succ :: (Enum a) => a -> a pred :: (Enum a) => a -> a toEnum :: (Enum a) => Int -> a fromEnum :: (Enum a) => a -> Int enumFrom :: (Enum a) => a -> [a] enumFromThen :: (Enum a) => a -> a -> [a] enumFromTo :: (Enum a) => a -> a -> [a] enumFromThenTo :: (Enum a) => a -> a -> a -> [a] -- | 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 /=. class Eq a (==) :: (Eq a) => a -> a -> Bool (/=) :: (Eq a) => a -> a -> Bool -- | File and directory names are values of type String, 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. type FilePath = String -- | 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. data Float :: * -- | Trigonometric and hyperbolic functions and related functions. -- -- Minimal complete definition: pi, exp, log, -- sin, cos, sinh, cosh, asin, -- acos, atan, asinh, acosh and atanh class (Fractional a) => Floating a pi :: (Floating a) => a exp :: (Floating a) => a -> a sqrt :: (Floating a) => a -> a log :: (Floating a) => a -> a (**) :: (Floating a) => a -> a -> a logBase :: (Floating a) => a -> a -> a sin :: (Floating a) => a -> a tan :: (Floating a) => a -> a cos :: (Floating a) => a -> a asin :: (Floating a) => a -> a atan :: (Floating a) => a -> a acos :: (Floating a) => a -> a sinh :: (Floating a) => a -> a tanh :: (Floating a) => a -> a cosh :: (Floating a) => a -> a asinh :: (Floating a) => a -> a atanh :: (Floating a) => a -> a acosh :: (Floating a) => a -> a -- | Fractional numbers, supporting real division. -- -- Minimal complete definition: fromRational and (recip or -- (/)) class (Num a) => Fractional a (/) :: (Fractional a) => a -> a -> a recip :: (Fractional a) => a -> a fromRational :: (Fractional a) => Rational -> a -- | The Functor class is used for types that can be mapped over. -- Instances of Functor should satisfy the following laws: -- -- fmap id == id fmap (f . g) == fmap f . fmap g -- -- The instances of Functor for lists, Data.Maybe.Maybe and -- System.IO.IO defined in the Prelude satisfy these laws. class Functor f :: (* -> *) fmap :: (Functor f) => (a -> b) -> f a -> f b -- | A value of type IO a is a computation which, when -- performed, does some I/O before returning a value of type a. -- -- There is really only one way to "perform" an I/O action: bind it to -- Main.main in your program. When your program is run, the I/O -- will be performed. It isn't possible to perform I/O from an arbitrary -- function, unless that function is itself in the IO monad and -- called at some point, directly or indirectly, from Main.main. -- -- IO is a monad, so IO actions can be combined using -- either the do-notation or the >> and >>= operations from -- the Monad class. data IO a :: * -> * -- | The Haskell 98 type for exceptions in the IO monad. Any I/O -- operation may raise an IOError instead of returning a result. -- For a more general type of exception, including also those that arise -- in pure code, see Control.Exception.Exception. -- -- In Haskell 98, this is an opaque type. type IOError = IOException -- | 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 using Prelude.minBound and Prelude.maxBound from the -- Prelude.Bounded class. data Int :: * -- | Arbitrary-precision integers. data Integer :: * -- | Integral numbers, supporting integer division. -- -- Minimal complete definition: quotRem and toInteger class (Real a, Enum a) => Integral a quot :: (Integral a) => a -> a -> a rem :: (Integral a) => a -> a -> a div :: (Integral a) => a -> a -> a mod :: (Integral a) => a -> a -> a quotRem :: (Integral a) => a -> a -> (a, a) divMod :: (Integral a) => a -> a -> (a, a) toInteger :: (Integral a) => a -> Integer -- | 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 Data.Either.Either type. data Maybe a :: * -> * Nothing :: Maybe a Just :: a -> Maybe a -- | The Monad class defines the basic operations over a -- monad, a concept from a branch of mathematics known as -- category theory. From the perspective of a Haskell programmer, -- however, it is best to think of a monad as an abstract datatype -- of actions. Haskell's do expressions provide a convenient -- syntax for writing monadic expressions. -- -- Minimal complete definition: >>= and return. -- -- Instances of Monad should satisfy the following laws: -- -- return a >>= k == k a m >>= return == m m >>= -- (\x -> k x >>= h) == (m >>= k) >>= h -- -- Instances of both Monad and Functor should additionally -- satisfy the law: -- -- fmap f xs == xs >>= return . f -- -- The instances of Monad for lists, Data.Maybe.Maybe and -- System.IO.IO defined in the Prelude satisfy these laws. class Monad m :: (* -> *) (>>=) :: (Monad m) => m a -> (a -> m b) -> m b (>>) :: (Monad m) => m a -> m b -> m b return :: (Monad m) => a -> m a fail :: (Monad m) => String -> m a -- | Basic numeric class. -- -- Minimal complete definition: all except negate or (-) class (Eq a, Show a) => Num a (+) :: (Num a) => a -> a -> a (*) :: (Num a) => a -> a -> a (-) :: (Num a) => a -> a -> a negate :: (Num a) => a -> a abs :: (Num a) => a -> a signum :: (Num a) => a -> a fromInteger :: (Num a) => Integer -> a -- | 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. class (Eq a) => Ord a compare :: (Ord a) => a -> a -> Ordering (<) :: (Ord a) => a -> a -> Bool (>=) :: (Ord a) => a -> a -> Bool (>) :: (Ord a) => a -> a -> Bool (<=) :: (Ord a) => a -> a -> Bool max :: (Ord a) => a -> a -> a min :: (Ord a) => a -> a -> a data Ordering :: * LT :: Ordering EQ :: Ordering GT :: Ordering -- | Arbitrary-precision rational numbers, represented as a ratio of two -- Integer values. A rational number may be constructed using the -- % operator. type Rational = Ratio Integer -- | Parsing of Strings, producing values. -- -- Minimal complete definition: readsPrec (or, for GHC only, -- readPrec) -- -- Derived instances of Read make the following assumptions, which -- derived instances of Text.Show.Show obey: -- -- -- -- For example, given the declarations -- -- infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a -- -- the derived instance of Read 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 = 5 -- -- Note 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 class Read a readsPrec :: (Read a) => Int -> ReadS a readList :: (Read a) => ReadS [a] -- | A parser for a type a, represented as a function that takes a -- String and returns a list of possible parses as -- (a,String) pairs. -- -- Note that this kind of backtracking parser is very inefficient; -- reading a large structure may be quite slow (cf ReadP). type ReadS a = String -> [(a, String)] class (Num a, Ord a) => Real a toRational :: (Real a) => a -> Rational -- | Efficient, machine-independent access to the components of a -- floating-point number. -- -- Minimal complete definition: all except exponent, -- significand, scaleFloat and atan2 class (RealFrac a, Floating a) => RealFloat a floatRadix :: (RealFloat a) => a -> Integer floatDigits :: (RealFloat a) => a -> Int floatRange :: (RealFloat a) => a -> (Int, Int) decodeFloat :: (RealFloat a) => a -> (Integer, Int) encodeFloat :: (RealFloat a) => Integer -> Int -> a exponent :: (RealFloat a) => a -> Int significand :: (RealFloat a) => a -> a scaleFloat :: (RealFloat a) => Int -> a -> a isNaN :: (RealFloat a) => a -> Bool isInfinite :: (RealFloat a) => a -> Bool isDenormalized :: (RealFloat a) => a -> Bool isNegativeZero :: (RealFloat a) => a -> Bool isIEEE :: (RealFloat a) => a -> Bool atan2 :: (RealFloat a) => a -> a -> a -- | Extracting components of fractions. -- -- Minimal complete definition: properFraction class (Real a, Fractional a) => RealFrac a properFraction :: (RealFrac a, Integral b) => a -> (b, a) truncate :: (RealFrac a, Integral b) => a -> b round :: (RealFrac a, Integral b) => a -> b ceiling :: (RealFrac a, Integral b) => a -> b floor :: (RealFrac a, Integral b) => a -> b -- | Conversion of values to readable Strings. -- -- Minimal complete definition: showsPrec or show. -- -- Derived instances of Show have the following properties, which -- are compatible with derived instances of Text.Read.Read: -- -- -- -- For example, given the declarations -- -- infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a -- -- the derived instance of Show 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, -- -- class Show a showsPrec :: (Show a) => Int -> a -> ShowS show :: (Show a) => a -> String showList :: (Show a) => [a] -> ShowS -- | The shows functions return a function that prepends the -- output String to an existing String. This allows -- constant-time concatenation of results using function composition. type ShowS = String -> String -- | A String is a list of characters. String constants in Haskell -- are values of type String. type String = [Char] -- | raise a number to a non-negative integral power (^) :: (Num a, Integral b) => a -> b -> a -- | raise a number to an integral power (^^) :: (Fractional a, Integral b) => a -> b -> a -- | The computation appendFile file str function appends -- the string str, to the file file. -- -- Note that writeFile and appendFile write a literal -- string to a file. To write a value of any printable type, as with -- print, use the show function to convert the value to a -- string first. -- -- main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]]) -- appendFile :: FilePath -> String -> IO () -- | asTypeOf is a type-restricted version of const. It is -- usually used as an infix operator, and its typing forces its first -- argument (which is usually overloaded) to have the same type as the -- second. asTypeOf :: a -> a -> a -- | The catch function establishes a handler that receives any -- IOError raised in the action protected by catch. An -- IOError is caught by the most recent handler established by -- catch. These handlers are not selective: all IOErrors -- are caught. Exception propagation must be explicitly provided in a -- handler by re-raising any unwanted exceptions. For example, in -- -- f = catch g (\e -> if IO.isEOFError e then return [] else -- ioError e) -- -- the function f returns [] when an end-of-file -- exception (cf. isEOFError) occurs in g; otherwise, the -- exception is propagated to the next outer handler. -- -- When an exception propagates outside the main program, the Haskell -- system prints the associated IOError value and exits the -- program. -- -- Non-I/O exceptions are not caught by this variant; to catch all -- exceptions, use Control.Exception.catch from Control.Exception. catch :: IO a -> (IOError -> IO a) -> IO a -- | Constant function. const :: a -> b -> a -- | curry converts an uncurried function to a curried function. curry :: ((a, b) -> c) -> a -> b -> c -- | 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. either :: (a -> c) -> (b -> c) -> Either a b -> c -- | error stops execution and displays an error message. error :: [Char] -> a even :: (Integral a) => a -> Bool -- | flip f takes its (first) two arguments in the reverse -- order of f. flip :: (a -> b -> c) -> b -> a -> c -- | general coercion from integral types fromIntegral :: (Integral a, Num b) => a -> b -- | Extract the first component of a pair. fst :: (a, b) -> a -- | gcd x y is the greatest (positive) integer that -- divides both x and y; for example gcd -- (-3) 6 = 3, gcd (-3) (-6) = 3, -- gcd 0 4 = 4. gcd 0 0 raises a -- runtime error. gcd :: (Integral a) => a -> a -> a -- | Read a character from the standard input device (same as -- hGetChar stdin). getChar :: IO Char -- | The getContents operation returns all user input as a single -- string, which is read lazily as it is needed (same as -- hGetContents stdin). getContents :: IO String -- | Read a line from the standard input device (same as hGetLine -- stdin). getLine :: IO String -- | Identity function. id :: a -> a -- | The interact function takes a function of type -- String->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. interact :: (String -> String) -> IO () -- | Raise an IOError in the IO monad. ioError :: IOError -> IO a -- | lcm x y is the smallest positive integer that both -- x and y divide. lcm :: (Integral a) => a -> a -> a -- | The lex 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 lex "" = [("","")].) If there is -- no legal lexeme at the beginning of the input string, lex fails -- (i.e. returns []). -- -- This lexer is not completely faithful to the Haskell lexical syntax in -- the following respects: -- -- lex :: ReadS String -- | 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. maybe :: b -> (a -> b) -> Maybe a -> b -- | Boolean "not" not :: Bool -> Bool odd :: (Integral a) => a -> Bool -- | otherwise is defined as the value True. It helps to make -- guards more readable. eg. -- -- f x | x < 0 = ... | otherwise = ... otherwise :: Bool -- | The print function outputs a value of any printable type to the -- standard output device. Printable types are those that are instances -- of class Show; print converts values to strings for -- output using the show 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]]) print :: (Show a) => a -> IO () -- | Write a character to the standard output device (same as -- hPutChar stdout). putChar :: Char -> IO () -- | Write a string to the standard output device (same as hPutStr -- stdout). putStr :: String -> IO () -- | The same as putStr, but adds a newline character. putStrLn :: String -> IO () -- | The read function reads input from a string, which must be -- completely consumed by the input process. read :: (Read a) => String -> a -- | The readFile function reads a file and returns the contents of -- the file as a string. The file is read lazily, on demand, as with -- getContents. readFile :: FilePath -> IO String -- | The readIO function is similar to read except that it -- signals parse failure to the IO monad instead of terminating -- the program. readIO :: (Read a) => String -> IO a -- | The readLn function combines getLine and readIO. readLn :: (Read a) => IO a -- | readParen True p parses what p parses, -- but surrounded with parentheses. -- -- readParen False p parses what p -- parses, but optionally surrounded with parentheses. readParen :: Bool -> ReadS a -> ReadS a -- | equivalent to readsPrec with a precedence of 0. reads :: (Read a) => ReadS a -- | general coercion to fractional types realToFrac :: (Real a, Fractional b) => a -> b -- | Evaluates its first argument to head normal form, and then returns its -- second argument as the result. seq :: a -> b -> b -- | utility function converting a Char to a show function that -- simply prepends the character unchanged. showChar :: Char -> ShowS -- | utility function that surrounds the inner show function with -- parentheses when the Bool parameter is True. showParen :: Bool -> ShowS -> ShowS -- | utility function converting a String to a show function that -- simply prepends the string unchanged. showString :: String -> ShowS -- | equivalent to showsPrec with a precedence of 0. shows :: (Show a) => a -> ShowS -- | Extract the second component of a pair. snd :: (a, b) -> b -- | the same as flip (-). -- -- Because - is treated specially in the Haskell grammar, -- (- e) is not a section, but an application of -- prefix negation. However, (subtract -- exp) is equivalent to the disallowed section. subtract :: (Num a) => a -> a -> a -- | uncurry converts a curried function to a function on pairs. uncurry :: (a -> b -> c) -> (a, b) -> c -- | A special case of error. It is expected that compilers will -- recognize this and insert error messages which are more appropriate to -- the context in which undefined appears. undefined :: a -- | until p f yields the result of applying f -- until p holds. until :: (a -> Bool) -> (a -> a) -> a -> a -- | Construct an IOError value with a string describing the error. -- The fail method of the IO instance of the Monad -- class raises a userError, thus: -- -- instance Monad IO where ... fail s = ioError (userError s) userError :: String -> IOError -- | The computation writeFile file str function writes the -- string str, to the file file. writeFile :: FilePath -> String -> IO () -- | Boolean "or" (||) :: Bool -> Bool -> Bool