\documentclass[12pt]{article}
\usepackage{fullpage,amsmath,amssymb}

%\setlength{\voffset}{1 in}

\setlength{\parskip}{1 ex}
\setlength{\parindent}{0 ex}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{center}
\textbf{Some common list functions}
\end{center}

1. Selecting parts of lists

\texttt{%
head :: [a] -> a
\ \ \textrm{return the first element of a list}
}

\texttt{%
tail ::\ [a] -> [a]
\ \ \textrm{return all but the first element of a non-empty list}
}

\texttt{%
last :: [a] -> a
\ \ \textrm{return the last element of a list}
}

\texttt{%
init :: [a] -> [a]
\ \ \textrm{return all but the last element of a non-empty list}
}

\texttt{%
take ::\ Int -> [a] -> [a]
\ \ \textrm{return the first n elements of a list}\newline
take 5 ( iterate ($\backslash$x -> x\^\ 2) 2 ) = [2,4,16,256,65536]
}

\texttt{%
drop ::\ Int -> [a] -> [a]
\ \ \textrm{drops the first terms of a list}\newline
drop 2 [[5,2,7],[ ],[0,1],[1..3]] = [[0,1],[1,2,3]]
}

\bigskip

2. Getting information about a list

\texttt{%
length ::\ [a] -> Int
}

\texttt{%
elem ::\ Eq a => a -> [a] -> Bool
\ \ \textrm{tell whether the element is a term of the list}\newline
elem [ ] [[5,2,7],[ ],[0,1],[1..3]] = True
}

\texttt{%
(!!) :: [a] -> Int -> a
\ \ \textrm{get the $n^{th}$ element of a list, where the first element is the
$0^{th}$}
\newline [1..10]!!7 = 8
}

\texttt{%
and ::\ [Bool] -> Bool
\ \ \textrm{logical conjunction}
}

\texttt{%
or ::\ [Bool] -> Bool
\ \ \textrm{logical disjunction}
}

\texttt{%
sum ::\ Num a => [a] -> a
}

\texttt{%
product ::\ Num a => [a] -> a
}

\bigskip

3. Combining lists

\texttt{%
(++) ::\ [a] -> [a] -> [a]
\ \ \textrm{join two lists into one}\newline
[5,2,7] ++ [0,1] = [5,2,7,0,1]
}

\texttt{%
concat ::\ [[a]] -> [a]
\ \ \textrm{join list of lists into one}\newline
concat [[5,2,7],[ ],[0,1],[1..3]] = [5,2,7,0,1,1,2,3]
}

\texttt{%
zip ::\ [a] -> [b] -> [(a,b)]
\ \ \textrm{return pairs of corresponding elements of two lists}\newline
zip [1..4] "abcdefgh" = [(1,'a'),(2,'b'),(3,'c'),(4,'d')]
}

\texttt{%
unzip ::\ [(a,b)] -> ([a],[b])
\ \ \textrm{reverses the zipping process}\newline
unzip [(1,'a'),(2,'b'),(3,'c'),(4,'d')] = ([1,2,3,4],"abcd")
}

\bigskip

\newpage
4. Creating and manipulating lists

\texttt{%
replicate :: Int -> a -> [a]
\ \ \textrm{make a list of copies of one element}\newline
replicate 3 'Z' = "ZZZ"
}

\texttt{%
reverse ::\ [a] -> [a]
\ \ \textrm{return list in reverse order}
}

\texttt{%
sort ::\ Ord a => [a] -> [a]
\ \ \textrm{(from the \texttt{List} library) return a sorted list}
}

\texttt{%
splitAt ::\ Int -> [a] -> ([a],[a])
\ \ \textrm{split the list into the first $n$ and the rest}\newline
splitAt 4 "abcdefg" = ("abcd","efg")
}

\texttt{%
nub :: Eq a => [a] -> [a]
\ \ \textrm{(from the \texttt{List} library) remove duplicates}\newline
nub [1,3,1,4,3,3] = [1,3,4]
}

\texttt{%
iterate ::\ (a -> a) -> a -> [a]
\ \textrm{return an infinite list} [x, f(x), f(f(x)),~...~]\newline
take 5 ( iterate ($\backslash$x -> x\^\ 2) 2 ) = [2,4,16,256,65536]
}

\bigskip

5. Using functions on lists

\texttt{%
map :: (a -> b) -> [a] -> [b]
\ \ \textrm{apply a function to each term of a list}\newline
map sqrt [1..5] = [1.0, 1.41421, 1.73205, 2.0, 2.23607]
}

\texttt{%
filter ::\ (a -> Bool) -> [a] -> [a]
\ \ \textrm{select elements of a list that satisfy a boolean function}\newline
filter ($\backslash$x -> length x > 2) [[5,2,7],[ ],[0,1],[1..3]] = [[5,2,7],[1,2,3]]
}

\texttt{%
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
\ \ \textrm{zip, then apply a function to each pair}\newline
zipWith (*) [2,3,4] [5,5,0] = [10,15,0]
}

\texttt{%
takeWhile :: (a -> Bool) -> [a] -> [a]
\ \ \textrm{returns a list containing elements from the front of the list
while the condition is satisfied.}\newline
takeWhile (<1000) ( iterate ($\backslash$x -> 2*x) 2 ) = [2,4,8,16,32,64,128,256,512]
}

\texttt{%
foldr1 :: (a -> a -> a) -> [a] -> a
\ \ \textrm{``fold'' the list starting at the right}\newline
foldr1 (-) [1,2,3,4] = (-2) \textrm{(calculates 1-(2-(3-4))~)}
}

\texttt{%
foldr :: (a -> b -> b) -> b -> [a] -> b
\ \ \textrm{``fold'' the list starting at the right, using a starting
  value}
\newline foldr (-) 5 [1,2,3,4] = 3 \textrm{(calculates 1-(2-(3-(4-5)))~)}
\newline foldr (+) 0 = sum
\newline foldr (++) [ ] = concat
\newline foldr (\&\&) True = and
\newline foldr ((:).f) [ ] = map f
\newline foldr ($\backslash$x xs -> if p x then x:xs else xs) [ ] = filter p
}

\end{document}

