Sunday 1 May 2016

Haskell: libraries


Haskell libraries are grouped into packages, each packages can contain zero (or) more modules. A module can contain any number of functions.

How to load a module?
By using import statement, you can import a module.

For example,
import Data.Char ( toUpper )

Above statement load the function toUpper from module Data.Char. 
Prelude> import Data.Char ( toUpper )
Prelude Data.Char> 
Prelude Data.Char> :t toUpper
toUpper :: Char -> Char
Prelude Data.Char> 
Prelude Data.Char> toUpper 'a'
'A'
Prelude Data.Char> 
Prelude Data.Char> toUpper 'd'
'D'
Prelude Data.Char> 
Prelude Data.Char> toUpper 'A'
'A'
Prelude Data.Char>


If you want to load complete module remove the ‘toUpper’ argument while importing, ‘import Data.Char ()’ statement imports all the modules.
Prelude> import Data.Char ()
Prelude Data.Char> :t Data.Char.toUpper
Data.Char.toUpper :: Char -> Char
Prelude Data.Char> 
Prelude Data.Char> :t Data.Char.toLower
Data.Char.toLower :: Char -> Char
Prelude Data.Char> 
Prelude Data.Char> Data.Char.toUpper('a')
'A'
Prelude Data.Char> Data.Char.toLower('A')
'a'
Prelude Data.Char>


Where can I get all the published libraries of Haskell?
‘http://hackage.haskell.org/’ is the Haskell community's central package archive of open source software.  

How to install new package?
Haskell provides a program ‘cabal’, it is used to install/update packages.

Open terminal (command prompt, if you are using Windows), run following commands.

cabal update
cabal install gloss
  
Note:
You can also load a module from GHCi prompt using the command :m (or) :module.

Prelude> :m +Data.Ratio
Prelude Data.Ratio> 10 % 200
1 % 20


Previous                                                 Next                                                 Home

No comments:

Post a Comment