开发者

Some issues with immutable hash in Racket

I have the following dumb test code:

#lang racket
(define vars '('g1 'g2 'g3 'g1))
(define addrs '(123 456 789 012))

(define immhs (make-immutable-hasheq empty))

(define immhs* (for/fold ([imhs immhs]) ([var (in-list vars)] [addr (in-list addrs) ]) (hash-set imhs var addr )))

immhs*

(hash-ref immhs* 'g1)

The output is:

'#hasheq(('g1 . 123)开发者_Python百科 ('g2 . 456) ('g3 . 789) ('g1 . 12))
hash-ref: no value found for key: 'g1

Why can't the hash-ref reference to the 'g1? (it will also fail on 'g2, etc) Then I uses (hash-keys immhs*), it returns '('g1 'g2 'g3 'g1), where there is 'g1; and I use further (car (hash-keys immhs*)), it returns ''g1 ; then the question is that why there are two quote ' before?


The problem is exactly the two quotes that you see: x evaluates to whatever x is bound to, 'x evaluates to a symbol, and ''x evaluates to a quoted form -- 'x. Try this:

(define vars '(g1 g2 g3 g1))

and it will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜