我真的是Haskell的新手,我对Data.HashMap有严重的问题。我做错了什么?
以下是我的尝试:
> ghci -v
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
Glasgow Haskell Compiler, Version 6.12.1, for Haskell 98, stage 2 booted by GHC version 6.12.1
Using binary package database: /usr/lib/ghc-6.12.1/package.conf.d/package.cache
Using binary package database: /home/joni/.ghc/x86_64-linux-6.12.1/package.conf.d/package.cache
package utf8-string-0.3.4-15837bafc9579c596819d4381db0f19b is shadowed by package utf8-string-0.3.4-75a948ddecbeae79ab3ec3d9f9bcdb65
package vimirc-0.0.0.1-d41d8cd98f00b204e9800998ecf8427e is unusable due to missing or recursive dependencies:
utf8-string-0.3.4-15837bafc9579c596819d4381db0f19b
hiding package base-3.0.3.2 to avoid conflict with later version base-4.2.0.0
hiding package bytestring-0.9.1.5 to avoid conflict with later version bytestring-0.9.1.10
wired-in package ghc-prim mapped to ghc-prim-0.2.0.0-9d35c97e886f807a1e6d024aaa91dcec
wired-in package integer-gmp mapped to integer-gmp-0.2.0.0-9a51ffb34a83618a1a3d4e472b9977a0
wired-in package base mapped to base-4.2.0.0-2cc27b7e43511c4ca001642a7f77a8f6
wired-in package rts mapped to builtin_rts
wired-in package haskell98 mapped to haskell98-1.0.1.1-0fdaf3b26bc38c43ce8371edf538dbf6
wired-in package template-haskell mapped to template-haskell-2.4.0.0-bbc7c61990d2fe9d20be2deb924f833c
wired-in package dph-seq mapped to dph-seq-0.4.0-52cfd6db5fc09a2abf793cd6a856a392
wired-in package dph-par mapped to dph-par-0.4.0-b4f339fed900d7bc4b3db61526caf863
Hsc static flags: -static
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> t <- Data.HashTable.new (==) Data.HashTable.hashString
*** Parser:
*** Desugar:
*** Simplify:
*** CorePrep:
*** ByteCodeGen:
Prelude> Data.HashTable.insert t "Joni" 1
*** Parser:
<interactive>:1:31:
No instance for (Num GHC.Prim.Any)
arising from the literal `1' at <interactive>:1:31
Possible fix: add an instance declaration for (Num GHC.Prim.Any)
In the third argument of `Data.HashTable.insert', namely `1'
In the expression: Data.HashTable.insert t "Joni" 1
In the definition of `it': it = Data.HashTable.insert t "Joni" 1
Prelude> Data.HashTable.insert t "Joni" "1"
*** Parser:
<interactive>:1:31:
Couldn't match expected type `GHC.Prim.Any'
against inferred type `[Char]'
In the third argument of `Data.HashTable.insert', namely `"1"'
In the expression: Data.HashTable.insert t "Joni" "1"
In the definition of `it': it = Data.HashTable.insert t "Joni" "1"
答案 0 :(得分:2)
您必须告诉它要在哈希表中存储哪种类型。这有效:
t <- Data.HashTable.new (==) Data.HashTable.hashString :: IO (Data.HashTable.HashTable String Integer)
Data.HashTable.insert t "Joni" 1