series

  • Basic grammar for notes in the Haskell Guide to Fun Learning
  • Notes in the Haskell Guide to Fun Learning
  • Function of notes in the Haskell Guide to Fun Learning
  • Haskell’s Guide to Fun Learning notes of higher order functions
  • Module of notes in the Haskell Guide to Fun Learning
  • Custom types of notes in the Haskell Guide to Fun Learning
  • I/O notes from the Haskell Guide to Fun Learning

All of the functions, types, and typeclass mentioned so far are part of the Prelude module, which is automatically imported by default.

The import module

import ModuleNameImport module statements must be prevented before function definition
import Data.List (nub.sort) Import only two functions
import Data.List hiding (nub) Do not import nuB
import qualified Data.Map You can only use data.map. XXX to use functions
import qualified Data.Map as M Only m.xx can be used to use functions
Copy the code

After the import, all functions of the module go into the “global” namespace.

To see which module the function resides in, use Hoogle (www.haskell.org/hoogle/).

The statements for importing modules in GHCi are:

ghci> :m + Data.List Data.Map Date.Set
Copy the code

Details: Dots can be used for both namespaces and compositions. How do you tell the difference? A function reference is treated as a dot between a qualified import module name and a function without a space; Otherwise it will be treated as a combination of functions.

Data. The List module

  • Words – Take the words from the string and form a list of strings
  • Group/sort/tails/isPrefixOf/any/isInfixOf whether to contain
  • Foldl ‘foldl without delay
  • find / lookup

Example:

import Data.List 
wordNums: :String- > [(String.Int)] 
wordNums = map (\ws -> (head ws, length ws)) . group . sort . words
Copy the code

Data. Char module

  • ord ‘a’ — 97
  • chr 97 — ‘a’

Maybe the type

findKey: : (Eq k) => k -> [(k, v)] -> Maybe v 
findKey key [] = Nothing 
findKey key ((k, v): xs)    
    | key == x = Just v    
    | otherwise = findKey key xs 
Copy the code

Pay attention to Maybe/Nothing/Just.

Data. The Map module

  • API: fromList / insert / size / fromListWith

Use the sample

import qualified Data. Map as Map 
phoneBook: :Map. Map String String 
phoneBook = Map. fromList $    
[(" betty"."555-2938")     , (" bonnie"."452-2928")     , (" patsy"."493-2928")     , (" lucille"."205-2928")     , (" wendy"."939-8282")     , (" penny"."853-2492")]

ghci> :t Map. lookup
Map. lookup :: (Ord k) => k -> Map. Map k a -> Maybe a 
ghci> Map. lookup "betty" phoneBook 
Just "555-2938" 
ghci> Map. lookup "wendy" phoneBook 
Just "939-8282" 
ghci> Map. lookup "grace" phoneBook 
Nothing
Copy the code

Custom modules

Common modules

  1. The new Geometry. The hs

  2. Write files

    module Geometry ( sphereVolume , sphereArea ) where sphereVolume :: Float -> Float sphereVolume radius = (4.0/3.0) * PI * (radius ^ 3) sphereArea :: Float -> Float sphereArea radius = 4 * pi * (radius ^ 2)Copy the code
  3. Introduce the module Import Geometry into other files in the same directory

Hierarchical modules

  1. New Geometry Directory

  2. Create sphere. hs/cuboid. hs/cucu.hs in the Geometry directory

  3. These three files look something like this

    module Geometry.Sphere ( volume , area ) where volume :: Float -> Float volume RADIUS = (4.0/3.0) * PI * (RADIUS ^ 3) area :: Float -> Float area radius = 4 * pi * (radius ^ 2)Copy the code
  4. Import the module import Geometry.Sphere into the sibling file of the Geometry directory