Hmm… from the link that @sorawee posted, “In the latter case, the function that first-class-or
evaluates to is similar to or, but evaluates all its arguments.” first-class-or
doesn’t seem very useful, but maybe there are other identifier macros I would have a use for.
(apply first-class-or xs)
is the same as (ormap values xs)
.
I use the latter a lot.
I use ormap
and andmap
a lot also, but if they didn’t exist, I expect I’d write them as a function instead of an identifier macro.
How would you implement a 2D lookup table in Racket? Say you have a-z as rows, and 1–26 on each column, and you wanted to store some data in each Table[letter][number]. What’s the best way to do this? Nested hash tables? That seems not so good though, with those nested (hash-ref (hash-ref ...)
things will look pretty ugly
(hash-ref my-hash-table (cons 'a 1))
(hash-set! my-hash-table (cons 'a 1) 42)
The solution @sorawee posted is probably what you want, but as a quick example of a two dimensional vector in Racket: #lang racket
(define (construct-vec num-rows num-cols)
(let ([ vec (make-vector num-rows) ])
(for ([i (in-range num-rows)])
(vector-set! vec i (make-vector num-cols)))
vec))
(define (get-vec vec row col)
(vector-ref (vector-ref vec row) col))
(define (set-vec! vec row col val)
(vector-set! (vector-ref vec row) col val))
(define vec (construct-vec 26 26))
(set-vec! vec 7 8 "I live at row 7 col 8")
(displayln (get-vec vec 7 8))
In your example, you might need a function to convert a letter/symbol to an index e.g. (get-vec vec (index-of 'a) 8)