Create HashTable in Haskell

I want to create a HashTable in Haskell, insert the hash values ​​inside and look in this HashTable.

I found this documentation , but I just started Haskell, and therefore I really don't know how to use these functions.

If some of you could show me some lines of code, that would be great.

+4
source share
3 answers

I second Ingo comment on how to start with something simpler. However, I will break a few things a bit.

, , Haskell. - , . , , -, .

Data.HashTable, , , , . .

Haskell Haskell /:

Data.Map - . , :

  • .
  • - Data.Map , Haskell, .
  • ghci.
  • , containers, Data.Map ( cabal).

, - /:

module MyModule where

import Data.Map (Map)             -- This just imports the type name
import qualified Data.Map as Map  -- Imports everything else, but with names 
                                  -- prefixed with "Map." (with the period).

-- Example: make a Map from a key/value pair
ages :: Map String Integer
ages = Map.fromList [("Joe", 35), ("Mary", 37), ("Irma", 16)]

:

-- Example: look up somebody and return a message saying what their age is.
-- 'Nothing' means that the map didn't have the key.
findAge :: String -> String
findAge name = case Map.lookup ages name of
                 Nothing  -> "I don't know the age of " ++ name ++ "."
                 Just age -> name ++ " is " ++ show age ++ " years old."

-- Example: make a map with one extra entry compared to `ages` above.
moreAges :: Map String Integer
moreAges = Map.insert "Steve" 23 ages

-- Example: union of two maps.
evenMoreAges :: Map String Integer
evenMoreAges = Map.union moreAges anotherMap
    where anotherMap = Map.fromList [("Metuselah", 111), ("Anuq", 3)]
+10

Ingo Data.Map.

 import qualified Data.Map as M

 myMap :: M.Map Int String
 myMap = M.fromList $ zip [1..10] ['a'..'j']

 insertedMap :: M.Map Int String
 insertedMap = M.insert 11 "fizzbuzz" oldMap

 at11 :: Maybe String
 at11 = M.lookup 11 insertedMap

M.lookup, M.insert / . / ( , IO ). , -

  let newMap = M.insert key val oldMap
  in M.union oldMap otherMap

, -? "", .

+4

, - , , :

new :: (key -> key -> Bool) -> (key -> Int32) -> IO (HashTable key val)

: HashTable key , , , - . , eq hashit , :

new eq hashit

HashTable IO-Monad.

HashTable -:

 fromList hashInt [(42, "forty-two"), (0, "zero")]
+2
source

All Articles