We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
{- Some standard functions from Prelude Data.
List False && _ = False
[Link] [Link] [Link] -} True || _ = True ------------------------------------------------- False || x = x class Show a where show :: a -> String not :: Bool -> Bool not True = False class Read a where not False = True read :: String => a --- functions on Maybe -------------------------- class Eq a where isJust, isNothing :: Maybe a -> Bool (==), (/=) :: a -> a -> Bool isJust (Just a) = True isJust Nothing = False class (Eq a) => Ord a where isNothing = not . isJust (<), (<=), (>=), (>) :: a -> a -> Bool max, min :: a -> a -> a fromJust :: Maybe a -> a fromJust (Just a) = a class (Eq a, Show a) => Num a where (+), (-), (*) :: a -> a -> a maybeToList :: Maybe a -> [a] negate, abs, signum :: a -> a maybeToList Nothing = [] fromInteger :: Integer -> a maybeToList (Just a) = [a]
class (Num a, Ord a) => Real a where listToMaybe :: [a] -> Maybe a toRational :: a -> Rational listToMaybe [] = Nothing listToMaybe (a:_) = Just a class (Real a, Enum a) => Integral a where quot,rem,div,mod :: a -> a -> a catMaybes :: [Maybe a] -> [a] toInteger :: a -> Integer catMaybes ls = [x | Just x <- ls]
class (Num a) => Fractional a where -- functions on pairs ----------------------------
(/) :: a -> a -> a fst :: (a,b) -> a fromRational :: Rational -> a fst (x,y) = x
class (Fractional a) => Floating a where snd :: (a,b) -> b
exp, log, sqrt, sin, cos, tan :: a -> a snd (x,y) = y
class (Real a, Fractional a) => RealFrac a where swap :: (a,b) -> (b,a) truncate, round, ceiling, floor swap (a,b) = (b,a) :: (Integral b) => a -> b curry :: ((a, b) -> c) -> a -> b -> c -- numerical functions -------------------------- curry f x y = f (x, y) even, odd :: (Integral a) => a -> Bool even n = n `rem` 2 == 0 uncurry :: (a -> b -> c) -> ((a, b) -> c) odd = not . even uncurry f p = f (fst p) (snd p)
-- monadic functions ---------------------------- -- functions on lists ---------------------------
sequence :: Monad m => [m a] -> m [a] map :: (a -> b) -> [a] -> [b] -- eval actions from left to right, return results map f xs = [ f x | x <- xs ] mapM :: Monad m => (a -> m b) -> [a] -> m [b] -- apply action to every element in a list (++) :: [a] -> [a] -> [a] replicateM :: Monad m => Int -> m a -> m [a] xs ++ ys = foldr (:) ys xs -- performs action n times, and return a list filter :: (a -> Bool) -> [a] -> [a] -- functions on functions ----------------------- filter p xs = [ x | x <- xs, p x ] id :: a -> a id x = x concat :: [[a]] -> [a] const :: a -> b -> a concat xss = foldr (++) [] xss const x _ = x concatMap :: (a -> [b]) -> [a] -> [b] (.) :: (b -> c) -> (a -> b) -> a -> c concatMap f = concat . map f f . g = \ x -> f (g x) head, last :: [a] -> a flip :: (a -> b -> c) -> b -> a -> c head (x:_) = x flip f x y = f y x last [x] = x ($) :: (a -> b) -> a -> b last (_:xs) = last xs f $ x = f x tail, init :: [a] -> [a] ----- functions on Bool ------------------------- tail (_:xs) = xs (&&), (||) :: Bool -> Bool -> Bool True && x = x init [x] = []
12 init (x:xs) = x : init xs reverse :: [a] -> [a] null :: [a] -> Bool reverse = foldl (flip (:)) [] null [] = True null (_:_) = False and, or :: [Bool] -> Bool and = foldr (&&) True length :: [a] -> Int or = foldr (||) False length = foldr (const (1+)) 0 any, all :: (a -> Bool) -> [a] -> Bool (!!) :: [a] -> Int -> a any p = or . map p (x:_) !! 0 = x all p = and . map p (_:xs) !! n = xs !! (n-1) elem, notElem :: (Eq a) => a -> [a] -> Bool foldr :: (a -> b -> b) -> b -> [a] -> b elem x = any (== x) foldr f z [] = z notElem x = all (/= x) foldr f z (x:xs) = f x (foldr f z xs) lookup :: (Eq a) => a -> [(a,b)] -> Maybe b foldl :: (a -> b -> a) -> a -> [b] -> a lookup key [] = Nothing foldl f z [] = z lookup key ((x,y):xys) | key == x = Just y foldl f z (x:xs) = foldl f (f z x) xs | otherwise = lookup key xys
iterate :: (a -> a) -> a -> [a] sum, product :: (Num a) => [a] -> a iterate f x = x : iterate f (f x) sum = foldl (+) 0 product = foldl (*) 1 repeat :: a -> [a] repeat x = xs where xs = x:xs maximum, minimum :: (Ord a) => [a] -> a maximum [] = error "[Link]: empty list" replicate :: Int -> a -> [a] maximum (x:xs) = foldl max x xs replicate n x = take n (repeat x) minimum [] = error "[Link]: empty list" cycle :: [a] -> [a] minimum (x:xs) = foldl min x xs cycle [] = error "cycle: empty list" cycle xs = xs' where xs' = xs ++ xs' zip :: [a] -> [b] -> [(a,b)] zip = zipWith (,) tails :: [a] -> [[a]] tails xs = xs : case xs of zip3 :: [a] -> [b] -> [c] -> [(a,b,c)] [] -> [] _ : xs' -> tails xs' zipWith :: (a->b->c) -> [a]->[b]->[c] zipWith z (a:as) (b:bs) take, drop :: Int -> [a] -> [a] = z a b : zipWith z as bs take n _ | n <= 0 = [] zipWith _ _ _ = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs unzip :: [(a,b)] -> ([a],[b]) unzip = drop n xs | n <= 0 = xs foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[]) drop _ [] = [] drop n (_:xs) = drop (n-1) xs nub :: Eq a => [a] -> [a] nub [] = [] splitAt :: Int -> [a] -> ([a],[a]) nub (x:xs) = x : nub [ y | y <- xs, x /= y ] splitAt n xs = (take n xs, drop n xs) delete :: Eq a => a -> [a] -> [a] takeWhile, dropWhile :: (a -> Bool) -> [a] -> [a] delete y [] = [] takeWhile p [] = [] delete y (x:xs) = takeWhile p (x:xs) | p x = x : takeWhile p xs if x == y then xs else x : delete y xs | otherwise = [] (\\) :: Eq a => [a] -> [a] -> [a] dropWhile p [] = [] (\\) = foldl (flip delete) dropWhile p (x:xs) | p x = dropWhile p xs | otherwise = x:xs union :: Eq a => [a] -> [a] -> [a] union xs ys = xs ++ (ys \\ xs) span :: (a -> Bool) -> [a] -> ([a], [a]) span p as = (takeWhile p as, dropWhile p as) intersect :: Eq a => [a] -> [a] -> [a] intersect xs ys = [ x | x <- xs, x `elem` ys ] lines, words :: String -> [String] -- lines "apa\nbepa\ncepa\n" ==["apa","bepa","cepa"] intersperse :: a -> [a] -> [a] -- words "apa bepa\n cepa" == ["apa","bepa","cepa"] -- intersperse 0 [1,2,3,4] == [1,0,2,0,3,0,4]
13 intToDigit :: Int -> Char partition :: (a -> Bool) -> [a] -> ([a],[a]) -- intToDigit 3 == '3' partition p xs = (filter p xs, filter (not . p) xs) ord :: Char -> Int group :: Eq a => [a] -> [[a]] chr :: Int -> Char group = groupBy (==) -- functions from [Link] --------------- groupBy :: (a -> a -> Bool) -> [a] -> [[a]] arbitrary :: Arbitrary a => Gen a groupBy _ [] = [] -- generator used by quickCheck groupBy eq (x:xs) = (x:ys) : groupBy eq zs where (ys,zs) = span (eq x) xs choose :: Random a => (a, a) -> Gen a -- a random element in the given inclusive range isPrefixOf :: Eq a => [a] -> [a] -> Bool isPrefixOf [] _ = True oneof :: [Gen a] -> Gen a isPrefixOf _ [] = False -- Randomly uses one of the given generators isPrefixOf (x:xs) (y:ys) = x==y && isPrefixOf xs ys frequency :: [(Int, Gen a)] -> Gen a -- Chooses from weighted list of generators isSuffixOf :: Eq a => [a] -> [a] -> Bool isSuffixOf x y = reverse x `isPrefixOf` reverse y elements :: [a] -> Gen a -- Generates one of the given values. sort :: (Ord a) => [a] -> [a] sort = foldr insert [] listOf :: Gen a -> Gen [a] -- Generates a list of random length. insert :: (Ord a) => a -> [a] -> [a] vectorOf :: Int -> Gen a -> Gen [a] insert x [] = [x] -- Generates a list of the given length. insert x (y:xs) = if x <= y then x:y:xs else y:insert x xs sized :: (Int -> Gen a) -> Gen a -- construct generators that depend a size param -- functions on Char ---------------------------- type String = [Char] -- IO functions --------------------------------- putStr, putStrLn :: String -> IO () isSpace, isDigit, isAlpha :: Char -> Bool getLine :: IO String toUpper, toLower :: Char -> Char readLn :: Read a => IO a