© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_25

25. Haskell Libraries

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

In programming, libraries are collections of precompiled routines that can be used in other programs. Usually, a library stores frequently used routines (for example, reading or printing an input), or it is specialized for a particular subject (for example, the libraries used in the statistics field). In this chapter, you will learn about the main libraries in Haskell and when you can use them to create powerful programs.

Prelude

The main library in Haskell is Prelude, which is imported by default when you install GHC.

Prelude contains standard types and classes, operations on lists, conversion techniques from a value to a String and vice versa, basic input and output operations and functions, basic exception handling, and functions for types.

Here is an example that handles exceptions using error:
divide :: Float -> Float -> Float
divide x 0 = error "Cannot divide by 0"
divide x y = x / y
After loading and compiling, let’s test it.
Prelude> :load Division.hs
[1 of 1] Compiling Main             ( Division.hs, interpreted )
Ok, one module loaded.
*Main> divide 3 5
0.6
*Main> divide 10 0
*** Exception: Cannot divide by 0
CallStack (from HasCallStack):
  error, called at Division.hs:2:14 in main:Main

The previous example is simple. If the second argument of the division function is 0, then you print an error message; otherwise, you print the result of division between the two arguments.

Haskell 2010 Libraries

The language and library specification of Haskell 2010 contains a collection of libraries with basic functionalities included in all Haskell implementations. The libraries are maintained by the Haskell process, and you can find the complete list of Haskell 2010 libraries in [1].

Two important packages are base (which includes modules like Control.Monad, Data.List, and System.IO) and vector (which includes modules such as Data.Vector).

Because you have already worked with elements from base, this example involves vectors. To use functions from vector, you need to install it with the following command at a terminal:
cabal install vector
You can create a vector from a list, as shown here:
Prelude> import Data.Vector
Prelude Data.Vector> let a = fromList [5,6,7]
Prelude Data.Vector> a
[5,6,7]
Prelude Data.Vector> :t a
a :: Num a => Vector a
Here you can see which element has index 1 (note the indexing begins from 0):
Prelude Data.Vector> a ! 1
6

GHC Bootstrap Libraries

GHC Bootstrap libraries are an extension of the Haskell 2010 libraries, used to build GHC itself. Examples of such libraries are haskeline and integer-gmp., For this chapter, you don’t need packages from this collection.

Core Libraries and Haskell Platform Libraries

Core libraries are part of the management process, defining the basic APIs that can be used in any Haskell implementation, the packages for backward compatibility, or the packages that are needed to link things in the Haskell platform together. Examples of such libraries and packages are the Monad transformer library, random, and parallel.

The following is an example of using the random library , where randomRs generates a random sequence:
import System.Random
main = do
  g <- getStdGen
  print $ take 10 (randomRs ('a', 'z') g)

In addition to the core libraries, the Haskell platform libraries contain more complex packages such as attoparsec, network, and QuickCheck.

The Hackage Database

The Hackage database contains a huge list of libraries, specializing in a large range of subjects, such as blockchain, chemistry, files, hydrology, scientific simulation, and so on. You can find the complete list of packages grouped by subjects in [2].

Summary

In this chapter, you learned about classified packages and libraries in Haskell. Note that because Haskell is an open source platform, anyone can create libraries. A short tutorial of the processes involved in creating and submitting a library can be found in [3] or [4].

References

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.179.161