guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Weird behavior of hash-table


From: Zelphir Kaltstahl
Subject: Weird behavior of hash-table
Date: Sat, 23 Nov 2019 23:39:38 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Icedove/52.9.1

Hi Guile Users!

I've noticed a strange behavior of hash tables. I put in symbols as keys
and integers as values, but when I try to get the integers out again by
using the same symbol, I get back a #f instead. Here is the code I am using:

~~~~~~~~
(use-modules
 ;; SRFI 60: procedures for treating integers as bits
 (srfi srfi-60)
 (ice-9 hash-table))


(define SQUARES
  ;; vector, constant time access
  #('A1 'B1 'C1 'D1 'E1 'F1 'G1 'H1
    'A2 'B2 'C2 'D2 'E2 'F2 'G2 'H2
    'A3 'B3 'C3 'D3 'E3 'F3 'G3 'H3
    'A4 'B4 'C4 'D4 'E4 'F4 'G4 'H4
    'A5 'B5 'C5 'D5 'E5 'F5 'G5 'H5
    'A6 'B6 'C6 'D6 'E6 'F6 'G6 'H6
    'A7 'B7 'C7 'D7 'E7 'F7 'G7 'H7
    'A8 'B8 'C8 'D8 'E8 'F8 'G8 'H8))


(define square-pos->bit-integer
  (lambda (pos)
    (expt 2 pos)))


(define make-squares-to-integer-mapping
  (lambda (squares)
    (let* ([num-squares (vector-length squares)]
           [lookup-table (make-hash-table num-squares)])
      (let loop ([square-pos 0])
        (cond
         [(< square-pos num-squares)
          #;(display
           (simple-format
            #f "setting at key: ~a\n" (vector-ref squares square-pos)))
          (hash-set! lookup-table
                     (vector-ref squares square-pos)
                     (square-pos->bit-integer square-pos))
          (loop (+ square-pos 1))]
         [else lookup-table])))))


(define SQUARES-TO-INTEGERS
  (make-squares-to-integer-mapping SQUARES))


(hash-ref SQUARES-TO-INTEGERS 'A1)
~~~~~~~~

Surprisingly, I cannot get out any value of the ones stored in the hash
table. But when I use:

(hash-map->list cons SQUARES-TO-INTEGERS)

I can clearly see, that under the keys I have in SQUARES, are the
numbers stored in the hash table.

Also I use hash-ref, which is supposed to compare using equal?, which
for symbols like 'A1 and 'A1 returns #t, so I think my lookup should work.

Why can I not get the integers out of the hash table?

Regards,

Zelphir




reply via email to

[Prev in Thread] Current Thread [Next in Thread]