Haskell Modules are a collection of related functions, types and type classes.
Standard Haskell module is named prelude
Modules are used to manipulate lists, concurrent programming, complex numbers, etc.
why use modules
Modules becomes reusables if they are enough generic
Modules make programs loosely coupled
Modules make more manageable by having it split into several parts
How to used
Aggregate import clause and the name of Module at the beginning of your script
Add as imports as you need in separate lines
When you do import Data.List, all the functions that Data.List exports become available in the global namespace
From ghci terminal you may use :m + <name module>
Annotations:
|
If you just need a couple of functions from a module, you can selectively import just those functions.
You might use hiding to exclude functions within modeles
You can use qualified imports to deal with clashes and add an alias to imports
Haskell Included modules
Data.List
imterparse takes an element and a list and put the first arguent and put it between each element of the second argument list
Intercalate function receives a list and a list of list and intercalate each element
transpose function transposes a list of list
concat function flatten a list of list in a single list
concatMap: mapping a functionin a list and the concatenate
'and' and 'or' functions evaluates a predicate and return a boolean if condition accomplish
any and all take a predicate and then check if any or all the elements in a list satisfy the predicate
splitAt takes a number and a list. It then splits the list at that many elements
takeWhile takes elements from a list while the predicate holds and then when an element is encountered that doesn't satisfy the predicate, it's cut off
dropWhile drops all the elements while the predicate is true
Data.Map
Association lists (also called dictionaries)
are lists that are used to store key-value
pairs where ordering doesn't matter.
The fromList function takes an association list
empty represents an empty map. It takes no arguments, it just returns an empty map.
insert takes a key, a value and a map and returns a new map that's just like the old one, only with the
key and value inserted
null checks if a map is empty.
size reports the size of a map.
singleton takes a key and a value and creates a map that has exactly one mapping.
member is a predicate takes a key and a map and reports whether the key is in the map or not.
keys and elems return lists of keys and values respectively. keys is the equivalent of map fst .
Map.toList and elems is the equivalent of map snd . Map.toList.
fromListWith is a cool little function. It acts like fromList, only it doesn't discard duplicate keys but it
uses a function supplied to it to decide what to do with them. Let's say that a girl can have several
numbers and we have an association list set up like this.
insertWith is to insert what fromListWith is to fromList. It inserts a key-value pair into a map, but if
that map already contains the key, it uses the function passed to it to determine what to do.
Data.Char
The Data.Char module does what its name
suggests. It exports functions that deal with
characters. It's also helpful when filtering and
mapping over strings because they're just lists of
characters.
This module exports a
bunch of predicates over
characters
isControl checks whether a character is a control character.
isSpace checks whether a character is a white-space characters. That
includes spaces, tab characters, newlines, etc.
isLower and is Upper check is a character
is upper or Lower case
isAlpha checks whether a character is a letter.
isAlphaNum checks whether a character is a letter or a number.
isPrint checks whether a character is printable. Control characters, for instance, are not printable.
isDigit checks whether a character is a digit.
isLetter checks whether a character is a letter.
isNumber checks whether a character is numeric.
isSymbol checks whether a character is a fancy mathematical or currency symbol.
isSeparator checks for Unicode spaces and separators.
isAscii checks whether a character falls into the first 128 characters of the Unicode character set.
isAsciiUpper or isAsciiLower check if is an ASCII is upper or lower case
Data.Set
Sets are kind of like a cross between lists and maps. All the elements in a set are
unique. And because they're internally implemented with trees (much like maps in
Data.Map), they're ordered.
The fromList function works much like you would expect.
We can use the difference function to see which letters are in the first set but aren't in the second one and vice versa.
we can see all the unique letters used in both sentences by using union.
Making our own modules
Haskell allow us as many other lenguages to create our own modules
We should create a file named with *.hs, the fist letter of the file should be in upper case
At the beginning of a module, we specify the module name
Then we defined the functions the will be included within de module
Following we shold implement the functions we alredy define at the beginning of the module