Data.HashMap 问题 - 无法插入值

发布于 2024-11-16 04:06:46 字数 2690 浏览 4 评论 0原文

我真的是 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"

I'm really newbie with Haskell and I have serious problems with Data.HashMap. What I'm doing wrong?

Here is what I have tried:

> 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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-11-23 04:06:46

您必须告诉它要在哈希表中存储哪种类型。这有效:

t <- Data.HashTable.new (==) Data.HashTable.hashString :: IO (Data.HashTable.HashTable String Integer)
Data.HashTable.insert t "Joni" 1

You have to tell it which type you want to store in the hashtable. This works:

t <- Data.HashTable.new (==) Data.HashTable.hashString :: IO (Data.HashTable.HashTable String Integer)
Data.HashTable.insert t "Joni" 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文