Reputation: 189
I am trying to use assoc
in racket to create a memo table and want to associate an ordered pair (x,y) with a value but I am a little unclear of the syntax.
For example I have:
[f (lambda (x y)
(let ([ans (assoc [x y] memo)])
But it is not correct.
Upvotes: 2
Views: 5125
Reputation: 12023
To add, in professional-level Racket, you may also use hash tables to build a lookup table.
(define table (make-hash))
(hash-set! table 'password "location-of-treasure")
(printf "Where is the treasure? ~s\n" (hash-ref table 'password))
Upvotes: 3
Reputation: 236004
The assoc
procedure receives as its first parameter the "key" to the element that you're looking for, and as second parameter a list of associations - in this context, an association is just a key-value pair. It will return the first association that corresponds to the given key or #f
if none was found. For example:
(assoc 'x '((a 1) (b 2) (x 3) (c 4)))
> '(x 3)
If you need to use pairs as keys, that's fine and it will work like this:
(assoc (list 1 2) '(((1 2) x) ((3 4) y)))
> '((1 2) x)
Upvotes: 5