0% found this document useful (0 votes)
35 views4 pages

Reference Sheet For The Exam

This reference sheet provides definitions for standard functions and classes that may be required to answer exam questions. It includes functions for lists, booleans, numbers, strings, characters, and input/output. It also defines the Eq, Ord, Show, and Num type classes.

Uploaded by

dingomatches
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
35 views4 pages

Reference Sheet For The Exam

This reference sheet provides definitions for standard functions and classes that may be required to answer exam questions. It includes functions for lists, booleans, numbers, strings, characters, and input/output. It also defines the Eq, Ord, Show, and Num type classes.

Uploaded by

dingomatches
Copyright
© © All Rights Reserved
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
You are on page 1/ 4

Reference sheet for the exam

October 22, 2018

This sheet will be attached to the examination paper, and lists functions and classes that
may be required in answers to the examination questions.

1
Reference: selected standard functions
Basic functions
• odd, even :: Integral a => a -> Bool
Test whether a number is odd or even

• null :: [a] -> Bool


Test whether a list is empty

• head :: [a] -> a


The first element of a non-empty list

• tail :: [a] -> [a]


All but the first element of a non-empty list

• last :: [a] -> a


The last element of a non-empty list

• length :: [a] -> Int


The length of a list

• reverse :: [a] -> [a]


the reversal of a finite list

• (++) :: [a] -> [a] -> [a]


The concatenation of two lists.

• zip :: [a] -> [b] -> [(a,b)]


List of pairs of corresponding elements of two lists, stopping when one list runs out.

• take :: Int -> [a] -> [a]


The first n elements of the list if it has that many, otherwise the whole list.

• drop :: Int -> [a] -> [a]


The list without the first n elements if it has that many, otherwise the empty list.

• and :: [Bool] -> Bool


and returns True if all of the Booleans in the input list are True.

• or :: [Bool] -> Bool


or returns True if any of the Booleans in the input list are True.

• product :: Num a => [a] -> a


The product of a list of numbers.

• sum :: Num a => [a] -> a


The sum of a list of numbers.

• concat :: [[a]] -> [a]


The concatenation of a list of lists.
Higher order functions
• map :: (a -> b) -> [a] -> [b]
map f xs is the list obtained by applying f to each element of xs:

map f [x1 , x2 , . . .] = [f x1 , f x2 , . . . ]

• filter :: (a -> Bool) -> [a] -> [a]


filter p xs is the list of elements x of xs for which p x is True.

• iterate :: (a -> a) -> a -> [a]


iterate f x is the infinite list of repeated applications of f to x:

iterate f x = [x, f x, f (f x), . . . ]

• takeWhile :: (a -> Bool) -> [a] -> [a]


takeWhile p xs is the longest prefix of xs consisting of elements x for which p x is
True.

• dropWhile :: (a -> Bool) -> [a] -> [a]


dropWhile p xs is the rest of xs after removing takeWhile p xs.

Text processing
• words :: String -> [String]
breaks a string up into a list of words, which were delimited by white space.

• lines :: String -> [String]


breaks a string up into a list of strings at newline characters. The resulting strings do
not contain newlines.

• unwords :: [String] -> String


joins words, adding separating spaces.

• unlines :: [String] -> String


joins lines, after appending a terminating newline to each.

Character functions
• isAlpha :: Char -> Bool
tests whether a character is alphabetic (i.e. a letter).

• isUpper :: Char -> Bool


tests whether a character is an upper case letter.

• isLower :: Char -> Bool


tests whether a character is a lower case letter.

• isDigit :: Char -> Bool


tests whether a character is a digit.
• toUpper :: Char -> Char
converts lower case letters to upper case, and preserves all other characters.

• toLower :: Char -> Char


converts upper case letters to lower case, and preserves all other characters.

Input/Output
• getLine :: IO String
an action that reads a line from the console.

• putStrLn :: String -> IO ()


putStrLn s is an action that writes the string s, followed by a newline, to the console.

Selected standard classes


class Eq a where
(==), (/=) :: a -> a -> Bool

x /= y = not (x == y)
x == y = not (x /= y)

class (Eq a) => Ord a where


(<), (<=), (>=), (>) :: a -> a -> Bool

class Show a where


show :: a -> String

class (Eq a) => Num a where


(+), (-), (*) :: a -> a -> a
fromInteger :: Integer -> a

You might also like