Haskell:如何使用HashMap?

时间:2014-04-26 21:53:09

标签: haskell hashmap

我有一个关于haskell的程序,

我的预期结果如下:

*Main> unify [Var "x",Var "x"] [Obj "a", Obj "a"] NoBindings

Bindings fromList [("x",a)]

但是,这会导致以下错误:Not in scope: data constructor H.HashMap

我的代码如下:

import qualified Data.List as L
import qualified Data.HashMap.Strict as H
import qualified Data.HashSet as S


data Pattern = Var String
         | GVar Int
         | Obj String
         | Funct String [Pattern]
         | Prim String
 deriving Eq

data Bindings = Fail
          | NoBindings
          | Bindings (H.HashMap String Pattern)
 deriving Show


unify :: [Pattern] -> [Pattern] ->Bindings -> Bindings
unify _ _ Fail              = Fail

unify [Var x,Var x1] [Obj a,Obj a1] NoBindings
        | x==x1     = Bindings (H.HashMap x a)
            | otherwise = error $show x1

1 个答案:

答案 0 :(得分:3)

Data.HashMap.Strict模块公开数据类型HashMap,但不导出其数据构造函数。它导出emptysingleton函数:

empty :: HashMap k v
singleton :: (Hashable k) => k -> v -> HashMap k v

后者可用于您的情况:

unify [Var x,Var x1] [Obj a,Obj a1] NoBindings
        | x==x1     = Bindings (H.singleton x a)
        | otherwise = error $show x1