0% found this document useful (0 votes)
66 views10 pages

Haskell - Prelude I

This document defines common types and functions from the Haskell standard prelude. It defines types like Bool, Char, String, lists and Maybe. It also defines common functions that operate on these types like head, tail, null, elem, show, read and others.

Uploaded by

Roberto
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)
66 views10 pages

Haskell - Prelude I

This document defines common types and functions from the Haskell standard prelude. It defines types like Bool, Char, String, lists and Maybe. It also defines common functions that operate on these types like head, tail, null, elem, show, read and others.

Uploaded by

Roberto
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/ 10

Appendix A

Standard prelude

In this appendix we present some of the most commonly used definitions


from the standard prelude. For clarity, a number of the definitions have been
simplified or modified from those given in the Haskell Report (25).

A.1 Classes

Equality types:

class Eq a where
(==), (!=) :: a → a → Bool

x != y = ¬ (x == y )

Ordered types:

class Eq a ⇒ Ord a where


(<), (≤), (>), (≥) :: a → a → Bool
min , max :: a → a → a

min x y | x ≤ y = x
| otherwise = y
max x y | x ≤ y = y
| otherwise = x

Showable types:

class Show a where


show :: a → String

Readable types:

class Read a where


read :: String → a
A . 2 L O G I C A L VA L U E S 157

Numeric types:

class (Eq a , Show a ) ⇒ Num a where


(+), (−), (∗) :: a → a → a
negate , abs , signum :: a → a

Integral types:

class Num a ⇒ Integral a where


div , mod :: a → a → a

Fractional types:

class Num a ⇒ Fractional a where


(/) :: a → a → a
recip :: a → a

recip n = 1/n

Monadic types:

class Monad m where


return :: a → m a
(>
>=) :: m a → (a → m b ) → m b

A.2 Logical values

Type declaration:

data Bool = False | True


deriving (Eq , Ord , Show , Read )

Logical conjunction:

(∧) :: Bool → Bool → Bool


False ∧ = False
True ∧ b = b

Logical disjunction:

(∨) :: Bool → Bool → Bool


False ∨ b = b
True ∨ = True

Logical negation:

¬ :: Bool → Bool
¬ False = True
¬ True = False

Guard that always succeeds:

otherwise :: Bool
otherwise = True
158 S TA N DA R D P R E L U D E

A.3 Characters and strings

Type declarations:

data Char = ···


deriving (Eq , Ord , Show , Read )

type String = [ Char ]

Decide if a character is a lower-case letter:

isLower :: Char → Bool


isLower c = c ≥ ’a’ ∧ c ≤ ’z’

Decide if a character is an upper-case letter:

isUpper :: Char → Bool


isUpper c = c ≥ ’A’ ∧ c ≤ ’Z’

Decide if a character is alphabetic:

isAlpha :: Char → Bool


isAlpha c = isLower c ∨ isUpper c

Decide if a character is a digit:

isDigit :: Char → Bool


isDigit c = c ≥ ’0’ ∧ c ≤ ’9’

Decide if a character is alpha-numeric:

isAlphaNum :: Char → Bool


isAlphaNum c = isAlpha c ∨ isDigit c

Decide if a character is spacing:

isSpace :: Char → Bool


isSpace c = elem c " \t\n"

Convert a character to a Unicode number:

ord :: Char → Int


ord c = ···

Convert a Unicode number to a character:

chr :: Int → Char


chr n = ···

Convert a digit to an integer:

digitToInt :: Char → Int


digitToInt c | isDigit c = ord c − ord ’0’
A.4 NUMBERS 159

Convert an integer to a digit:

intToDigit :: Int → Char


intToDigit n
|n≥0∧n≤9 = chr (ord ’0’ + n )

Convert a letter to lower-case:

toLower :: Char → Char


toLower c | isUpper c = chr (ord c − ord ’A’ + ord ’a’)
| otherwise = c

Convert a letter to upper-case:

toUpper :: Char → Char


toUpper c | isLower c = chr (ord c − ord ’a’ + ord ’A’)
| otherwise = c

A.4 Numbers

Type declarations:

data Int = ···


deriving (Eq , Ord , Show , Read ,
Num , Integral )
data Integer = ···
deriving (Eq , Ord , Show , Read ,
Num , Integral )
data Float = ···
deriving (Eq , Ord , Show , Read ,
Num , Fractional )

Decide if an integer is even:

even :: Integral a ⇒ a → Bool


even n = n ‘mod ‘ 2 == 0

Decide if an integer is odd:

odd :: Integral a ⇒ a → Bool


odd = ¬ ◦ even

Exponentiation:

(↑) :: (Num a , Integral b ) ⇒ a → b → a


↑0 = 1
x ↑ (n + 1) = x ∗ (x ↑ n )
160 S TA N DA R D P R E L U D E

A.5 Tuples

Type declarations:

data () = ···
deriving (Eq , Ord , Show , Read )
data (a , b ) = ···
deriving (Eq , Ord , Show , Read )
data (a , b , c ) = ···
deriving (Eq , Ord , Show , Read )
..
.

Select the first component of a pair:

fst :: (a , b ) → a
fst (x , ) = x

Select the second component of a pair:

snd :: (a , b ) → b
snd ( , y ) = y

A.6 Maybe

Type declaration:

data Maybe a = Nothing | Just a


deriving (Eq , Ord , Show , Read )

A.7 Lists

Type declaration:

data [ a ] = [ ] | a : [a ]
deriving (Eq , Ord , Show , Read )

Decide if a list is empty:

null :: [ a ] → Bool
null [ ] = True
null ( : ) = False

Decide if a value is an element of a list:

elem :: Eq a ⇒ a → [ a ] → Bool
elem x xs = any (== x ) xs

Decide if all logical values in a list are True :

and :: [ Bool ] → Bool


A.7 LISTS 161

and = foldr (∧) True

Decide if any logical value in a list is False :

or :: [ Bool ] → Bool
or = foldr (∨) False

Decide if all elements of a list satisfy a predicate:

all :: (a → Bool ) → [ a ] → Bool


all p = and ◦ map p

Decide if any element of a list satisfies a predicate:

any :: (a → Bool ) → [ a ] → Bool


any p = or ◦ map p

Select the first element of a non-empty list:

head :: [ a ] → a
head (x : ) = x

Select the last element of a non-empty list:

last :: [ a ] → a
last [ x ] = x
last ( : xs ) = last xs

Select the n th element of a non-empty list:

(!!) :: [ a ] → Int → a
(x : ) !! 0 = x
( : xs ) !! (n + 1) = xs !! n

Select the first n elements of a list:

take :: Int → [ a ] → [ a ]
take 0 = []
take (n + 1) [ ] = []
take (n + 1) (x : xs ) = x : take n xs

Select all elements of a list that satisfy a predicate:

filter :: (a → Bool ) → [ a ] → [ a ]
filter p xs = [ x | x ← xs , p x ]

Select elements of a list while they satisfy a predicate:

takeWhile :: (a → Bool ) → [ a ] → [ a ]
takeWhile [ ] = []
takeWhile p (x : xs )
|px = x : takeWhile p xs
| otherwise = []

Remove the first element from a non-empty list:

tail :: [ a ] → [ a ]
162 S TA N DA R D P R E L U D E

tail ( : xs ) = xs

Remove the last element from a non-empty list:

init :: [ a ] → [ a ]
init [ ] = []
init (x : xs ) = x : init xs

Remove the first n elements from a list:

drop :: Int → [ a ] → [ a ]
drop 0 xs = xs
drop (n + 1) [ ] = []
drop (n + 1) ( : xs ) = drop n xs

Remove elements from a list while they satisfy a predicate:

dropWhile :: (a → Bool ) → [ a ] → [ a ]
dropWhile [ ] = []
dropWhile p (x : xs )
|px = dropWhile p xs
| otherwise = x : xs

Split a list at the n th element:

splitAt :: Int → [ a ] → ([ a ], [ a ])
splitAt n xs = (take n xs , drop n xs )

Split a list using a predicate:

span :: (a → Bool ) → [ a ] → ([ a ], [ a ])
span p xs = (takeWhile p xs , dropWhile p xs )

Process a list using an operator that associates to the right:

foldr :: (a → b → b ) → b → [ a ] → b
foldr v [ ] = v
foldr f v (x : xs ) = f x (foldr f v xs )

Process a non-empty list using an operator that associates to the right:

foldr1 :: (a → a → a ) → [ a ] → a
foldr1 [ x ] = x
foldr1 f (x : xs ) = f x (foldr1 f xs )

Process a list using an operator that associates to the left:

foldl :: (a → b → a ) → a → [ b ] → a
foldl v [ ] = v
foldl f v (x : xs ) = foldl f (f v x ) xs

Process a non-empty list using an operator that associates to the left:

foldl1 :: (a → a → a ) → [ a ] → a
foldl1 f (x : xs ) = foldl f x xs
A.7 LISTS 163

Produce an infinite list of identical elements:

repeat :: a → [ a ]
repeat x = xs where xs = x : xs

Produce a list with n identical elements:

replicate :: Int → a → [ a ]
replicate n = take n ◦ repeat

Produce an infinite list by iterating a function over a value:

iterate :: (a → a ) → a → [ a ]
iterate f x = x : iterate f (f x )

Produce a list of pairs from a pair of lists:

zip :: [ a ] → [ b ] → [(a , b )]
zip [ ] = []
zip [ ] = []
zip (x : xs ) (y : ys ) = (x , y ) : zip xs ys

Calculate the length of a list:

length :: [ a ] → Int
length = foldl (λn → n + 1) 0

Calculate the sum of a list of numbers:

sum :: Num a ⇒ [ a ] → a
sum = foldl (+) 0

Calculate the product of a list of numbers:

product :: Num a ⇒ [ a ] → a
product = foldl (∗) 1

Calculate the minimum of a non-empty list:

minimum :: Ord a ⇒ [ a ] → a
minimum = foldl1 min

Calculate the maximum of a non-empty list:

maximum :: Ord a ⇒ [ a ] → a
maximum = foldl1 max

Append two lists:

(++) :: [ a ] → [ a ] → [ a ]
[ ] ++ ys = ys
(x : xs ) ++ ys = x : (xs ++ ys )

Concatenate a list of lists:

concat :: [[ a ]] → [ a ]
concat = foldr (++) [ ]
164 S TA N DA R D P R E L U D E

Reverse a list:

reverse :: [ a ] → [ a ]
reverse = foldl (λxs x → x : xs ) [ ]

Apply a function to all elements of a list:

map :: (a → b ) → [ a ] → [ b ]
map f xs = [ f x | x ← xs ]

A.8 Functions

Type declaration:

data a → b = ···

Identity function:

id :: a → a
id = λx → x

Function composition:

(◦) :: (b → c ) → (a → b ) → (a → c )
f ◦g = λx → f (g x )

Constant functions:

const :: a → (b → a )
const x = λ →x

Strict application:

($!) :: (a → b ) → a → b
f $! x = ···

Convert a function on pairs to a curried function:

curry :: ((a , b ) → c ) → (a → b → c )
curry f = λx y → f (x , y )

Convert a curried function to a function on pairs:

uncurry :: (a → b → c ) → ((a , b ) → c )
uncurry f = λ(x , y ) → f x y

A.9 Input/output

Type declaration:

data IO a = ···
A.9 INPUT/OUTPUT 165

Read a character from the keyboard:

getChar :: IO Char
getChar = ···

Read a string from the keyboard:

getLine :: IO String
getLine = do x ← getChar
if x == ’\n’ then
return ""
else
do xs ← getLine
return (x : xs )

Read a value from the keyboard:

readLn :: Read a ⇒ IO a
readLn = do xs ← getLine
return (read xs )

Write a character to the screen:

putChar :: Char → IO ()
putChar c = ···

Write a string to the screen:

putStr :: String → IO ()
putStr "" = return ()
putStr (x : xs ) = do putChar x
putStr xs

Write a string to the screen and move to a new line:

putStrLn :: String → IO ()
putStrLn xs = do putStr xs
putChar ’\n’

Write a value to the screen:

print :: Show a ⇒ a → IO ()
print = putStrLn ◦ show

Display an error message and terminate the program:

error :: String → a
error xs = ···

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy