[racket] Help needed representing data | 2d hashes?

From: YC (yinso.chen at gmail.com)
Date: Thu Aug 12 05:22:30 EDT 2010

2010/8/11 Horace Dynamite <horace.dynamite at gmail.com>

> # And now the the thing I can't figure out how to do, the programmer
> creates a 2d hash, retaining the "values" column.
> %CODE = ( A => { map { $CODE_CHARS{A}[$_] => $_ } 0..106 },
>          B => { map { $CODE_CHARS{B}[$_] => $_ } 0..106 },
>          C => { map { $CODE_CHARS{C}[$_] => $_ } 0..106 } );
>


Are you asking how to retain the index (i.e. the $_, what you called
"values") along with the encodings?  The amount of Perl code you showed did
not do that (it just retains $_ without the encoding).

Assuming you want to retain both the encoding and value "together", a
*cons*will do:

(define (.. lower higher) ;; shorthand for perl's N..M generator syntax
  (build-list (- (+ higher 1) lower) (lambda (n) (+ n lower))))

(define A
  (make-hash (map (lambda (value)
                    (cons (list-ref 128-A value) ;; key
                          ;; encoding + value
                          *(cons (list-ref ENCODING n) value)*))
                  (.. 0 106))))
;; (define B ...)
;; (define C ...)



Then you just refer to the values...

(define (encoding-ref hash key)
  (*car* (hash-ref hash key)))

(define (value-ref hash key)
  (*cdr* (hash-ref hash key)))


Then you have

> (encoding-ref A #\G)
11010001000
> (value-ref A #\G)
39


Hope this is what you are looking for.

Cheers,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100812/cfd924da/attachment.html>

Posted on the users mailing list.