Prelude.hs is the standard library which is loaded when you start Haskell, and it contains all the functions you have learned so far. Here's my pick of the most useful. Note that just knowing about these functions ain't gonna help you; knowing how to use them is the key! So, lots of practice is essential.
List processing basics|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
No examples needed here I hope :)
1 - :
The basic list constructor. Adds an element to the front of a list.
2- ++
The list concatenator. Adds a list onto the front of another list. Make sure
you understand the difference between : and ++
3- head
Returns the
first element of a list.
4 - last
The opposite of head; last returns the last element of a list.
5 - tail
The tail of a list is everything except the first element. Returns an error
if the list is empty.
6 - init
The opposite of tail. Given a list, init returns the list without the
last element.
7 - length
Returns the length of the list
8 - !!
Returns the element of a list located at the specified index. Note that
an 'index' starts counting from zero.
1 - maximum and
minimum
These return the largest
and smallest elements of a list repectively. Don't worry too much about the Ord
a part of the type signature, but if you must know, then it means
that this function only takes inputs of a Type which is deriving
Ord. (1) That
means that Haskell knows how to put these things in Order. (e.g. 1 < 2, 2
< 5, 'a' < 'z', False < True)
2 - reverse
reverse takes a list
and, um, reverses it
3 - elem and notElem
elem
tells you if a specified
element is in a list, and notElem is simply the opposite of that. Note that
this only works for types which are deriving
Eq, (2) meaning that Haskell knows how to say "that
element is the same as this one".
4 - concat
concat takes in a list
of lists and combines them all into one list. (3)
5 - take and drop
take n [a]
gives you the first n elements of the list. Likewise, drop
n [a] gives you everything back except the first n elements
of a list.
6 - takeWhile and
dropWhile
takeWhile and dropWhile
are similar to normal take and drop, but are more powerful - they take in
a function (only certain types though) and uses it to 'test' elements of the
list starting from the beginning. It will continue to take or drop elements
from the list until an element fails the test.
The function you feed takeWhile or dropWhile must be of type (a -> Bool). That is, it takes one input of any type and returns a Bool. Examples of valid functions are isVowel (which you wrote) or the inbuilt functions isUpper, isLower, isDigit, even, odd. Less obvious but most useful are (==) and (>) and (<).
7 - words and
unwords
words takes in a single String
and breaks it up wherever there are spaces, into a list of Strings. And unwords
does the opposite.
1 - `div` and `mod`
`div` is how many whole
times the first number can be divided by the second number. `mod` is the remainder
after the first number is divided by the second number. Note that `div` and
`mod` only process integers.(4)
2 - gcd and lcm
gcd stands for Greatest
Common Divisor of two numbers. lcm is the Lowest Common Multiple of two numbers.
Useful
for some Discrete Mathematics
algorithms, but not much else.
3 - even and odd
How many times have you written isEven and isOdd, not realising that they
were already inbulit?
3 - sum and product
Simply add up or multiply all the contents of a list of numbers
1 - map
Higher Order functions (important!!)
2 - filter
3 - fold
Map, fold and filter are so powerful, they've been put on their own page instead.
1 - show
show converts anything which has a Show function defined, into a String.
2 - read
read is the opposite of show; it reads in a String and converts it to whatever
type you specify. Note the way I've used it below!
3 - succ and pred
For types with an Enum function defined, succ gives the successor and pred
gives the predecessor.
Footnotes
(1) This works for types deriving Ord (where Haskell works it out
for you), or which have had Ord written by the user. Haskell normally puts elements
in the order in which you typed them in. For example if you had
data TextNumber = One
| Two | Four | Three
deriving Ord
Then Haskell would think that One is less than Two, Two is less
than Four, and Four is Less than Three. Solution: type them in properly or
write your own Ord function.
(2) Likewise, you can let Haskell define Eq automatically (deriving Eq) or you can write your own. This is true for all classes (Eq, Ord, Enum, Show, Read ...)
(3) Monads are complicated.... and not studied in COMP1011.
(4) `div` and `mod` are examples of what are called "infix"
operators. That is, you can put the function name between the inputs in forward
quotes.
So 17 `div` 6 is exactly the
same as div 17 6. Use whichever
you are more comfortable with (probably the infix version).