Saturday 30 April 2016

Haskell: Import a module in a source file


By using import statement, you can import a moudle into your haskell file.

For example, ‘import Data.List’ statement import Data.List module.

load_module.hs
import Data.List

getPermutations :: [a] -> [[a]]
getPermutations x = permutations x

*Main> :load load_module.hs
[1 of 1] Compiling Main             ( load_module.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getPermutations "abc"
["abc","bac","cba","bca","cab","acb"]
*Main> 
*Main> getPermutations "god"
["god","ogd","dog","odg","dgo","gdo"]
*Main> 
*Main> getPermutations "ptr"
["ptr","tpr","rtp","trp","rpt","prt"]


permutations’ function is defined in Data.List module, after importing the module, all the functions in the module are available in your source file.


Previous                                                 Next                                                 Home

No comments:

Post a Comment