Modules and Libraries in Haskell Programming
Haskell is a powerful functional programming language that emphasizes immutability, purity, and type safety. Understanding how to organize code into modules, import and utilize libraries, and leverage the Haskell standard library is essential for writing effective, reusable code. This guide covers key concepts like organizing code into modules, importing libraries, and testing small libraries in Haskell.
module ModuleName wheredefine ModuleNameimport ModuleNamemodule : ModuleNameexport keywordimport do in a Haskell module?
hiding clause in the import statementexclude keywordnoimport keywordimport ModuleName (function1, function2)import ModuleName only function1import ModuleName without function2import ModuleName selectivequalified keyword do in Haskell imports?
import keyword and specifying the libraryuse keyword.ghci fileghc --libraries command.cabal filestack install library-namecabal install library-namestack get library-nameghc -install library-namestack listghc -liststack show librariesstack ghc --list.cabal filestack.yamlghci.conflibconfig.yamlcabal tool do in Haskell?
import LibraryNameimport * from LibraryNameimport LibraryName as *import all from LibraryNameimport statement in Haskell allow you to do with a library?
Data.Maybe is used to extract the value from a Just?
fromJustunwrapgetValueextractControl.Monad module provide in Haskell?
Data.Map module in Haskell?
Control.Exception moduleData.Either moduleSystem.IO moduleData.Maybe moduleData.List module in Haskell?
concatappendsumfindIndexData.Either module work with in Haskell?
.cabal fileMakefile.haskell fileconfig.yamlHUnit testing frameworkTest.HUnit module is used to run tests in Haskell?
runTestTTrunTeststestAllexecuteTesthspec library in Haskell?
| Qno | Answer |
|---|---|
| 1 | a) To organize code into manageable units |
| 2 | a) module ModuleName where |
| 3 | a) Modularity and reusability of code |
| 4 | a) By declaring them in the module’s export list |
| 5 | a) It allows access to functions from another module |
| 6 | a) Modules are used to define functions and types in a separate namespace |
| 7 | a) The function is private and cannot be accessed outside the module |
| 8 | a) By using hiding clause in the import statement |
| 9 | a) import ModuleName (function1, function2) |
| 10 | a) It allows you to use functions from a module with a qualified name |
| 11 | a) By using the import keyword and specifying the library |
| 12 | a) Stack |
| 13 | a) By searching on Hackage |
| 14 | a) The library name in the .cabal file |
| 15 | a) stack install library-name |
| 16 | a) stack list |
| 17 | a) .cabal file |
| 18 | a) It manages package dependencies and builds projects |
| 19 | a) import LibraryName |
| 20 | a) It provides access to the library’s functions and types |
| 21 | a) It provides basic functionality for common tasks like data manipulation and I/O |
| 22 | a) Data.List |
| 23 | a) fromJust |
| 24 | a) System.IO |
| 25 | a) Functions for working with monads and chaining operations |
| 26 | a) It provides an implementation of associative arrays (maps) |
| 27 | a) Using the Control.Exception module |
| 28 | a) concat |
| 29 | a) Control.Concurrent |
| 30 | a) It works with values that can either be a success or a failure |
| 31 | a) By defining functions and placing them in a module |
| 32 | a) .cabal file |
| 33 | a) Using the HUnit testing framework |
| 34 | a) runTestTT |
| 35 | a) It provides a behavior-driven testing framework |