0

Here is my attempt in creating a hash table and adding a vector to it based on a list.

(define *function-table* (make-hash))
(define (function-get key)(hash-ref *function-table* key))
(define (function-put! key value)(hash-set! *function-table* key value))
(define arguments '(myVector 5))
(lambda(pair)(function-put! (car pair) (make-hash (cadr pair))) arguments)

I get the following message:

#<procedure>

Now I will try to query the hash-table by getting the size of the vector that it contains.

(vector-length (function-get myVector))

Error message:

myVector: undefined;
 cannot reference undefined identifier
  context...:
Daniel
  • 3
  • 2
  • Scheme does not have `hake-hash`, but [`make-hashtable`](http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-14.html) for R6RS and `make-hash-table` from [SRFI-125](https://srfi.schemers.org/srfi-125/srfi-125.html) that also is the new standard in R7RS-large yet to be finalized. – Sylwester Oct 10 '17 at 10:08

1 Answers1

0

In order to add a vector to a hash table, you need to actually create said vector, and add it. For example,

(define table (make-hash))         ;; create new mutable hash table
(define vec (make-vector 10 5))    ;; create new vector
(hash-set! table 'myVector vec)    ;; add vector to hash, key='myVector, value=vec
(hash-ref table 'myVector)         ;; retrieve hash value for specified key
#(5 5 5 5 5 5 5 5 5 5)

Note that when using a quoted list, such as when you defined arguments, the first element is actually a symbol, ie. 'myVector. So, to refer to it as your hash key, you should add the quote on the name as: (function-get 'myVector).

Consider the following:

(define *function-table* (make-hash))

(define (function-get key)
  (hash-ref *function-table* key))

(define (function-put! key value)
  (hash-set! *function-table* key value))

(define arguments (list 'myVector (make-vector 10 5)))

((lambda (pair)
   (function-put! (car pair) (cadr pair)))
 arguments)

then you can have:

(vector-length (function-get 'myVector))
=> 10

EDIT: If arguments is a list containing the name and size of the vector that needs to be created and added to the hash, then you can do the following:

(define arguments '(myVector 5))
((lambda (pair)
   (function-put! (car pair) (make-vector (cadr pair))))
 arguments)

then,

(function-get 'myVector)
=> #(0 0 0 0 0)
(vector-length (function-get 'myVector))
=> 5
assefamaru
  • 2,751
  • 2
  • 10
  • 14
  • I haven't really understood the definition of quote. I read some articles online and these say quote is used for turning something into a constant. Can you elaborate on what quote means? and why do you use two arguments in the make vector statement? – Daniel Oct 10 '17 at 06:08
  • Perhaps this will give you a good overview: [what is the difference between quote and list](https://stackoverflow.com/questions/34984552/what-is-the-difference-between-quote-and-list). – assefamaru Oct 10 '17 at 06:16
  • As for `make-vector`, I simply used `(make-vector 10 5)` as an example vector, whereby 10 specifies how many slots I want the vector to have initially, and 5 indicates each slot's value. See [`make-vector`](https://docs.racket-lang.org/reference/vectors.html?q=make-vector#%28def._%28%28quote._~23~25kernel%29._make-vector%29%29). You can initialize an empty vector using `vector` or doing `(make-vector 0)`. – assefamaru Oct 10 '17 at 06:22
  • My original problem is that I need to create a vector based on the size and name given by the pair argument. Can you help me with that? – Daniel Oct 10 '17 at 06:29
  • Sure, I've edited my answer, see EDIT section above. – assefamaru Oct 10 '17 at 06:40
  • is there any chance I can friend you on facebook? – Daniel Oct 10 '17 at 07:10
  • Sure you can :) – assefamaru Oct 10 '17 at 07:18
  • I put the link to my facebook on the post. Let me know when you add me, so I can get rid of it. Thank you so much for the help though. – Daniel Oct 10 '17 at 07:31