【问题标题】:How to create and add items to dictionary如何创建项目并将其添加到字典
【发布时间】:2019-06-11 13:17:35
【问题描述】:

我正在尝试使用以下代码创建一个字典或哈希表,其中键是字符串,值是整数:

(define dict #())
(dict-set! dict "bash" 1)
(displayln dict)
(dict-set! dict "racket" 1)
(displayln dict)

但是,它给出了以下错误:

dict-set!: contract violation
  expected: (dict-implements/c dict-set!)
  given: '#()
  in: the d argument of
      (->i
       ((d (dict-implements/c dict-set!))
        (k (d) (dict-key-contract d))
        (value (d) (dict-value-contract d)))
       (_r void?))
  contract from: <collects>/racket/dict.rkt

问题出在哪里,如何解决?

【问题讨论】:

    标签: dictionary hashmap racket


    【解决方案1】:

    问题在于文字 #() 是一个空向量。

    要制作可变哈希表,请使用(make-hash)

     (define dict (make-hash))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多