Hello,
I have some difficulties when trying to create a macro doing some pattern
matching on a hash-table and was hoping finding some help.
The macro is taking a hash-table as parameter and I would like to extract
the value for the key 'name.
I did several trials of my macro with what should be, for my understanding,
the same hash-table but got always a different result.
Suprisingly, when I'm trying the same partern matching in a simple "match"
at runtime, I always get the answer expected.
Here is my code:
(define-syntax (m stx)
(syntax-case stx ()
[(_ (hash-table 'name n m ...))
#'(symbol->string n)]
[(_ (hash-table x0 x1 ...))
#'"..."]
[(_ (hash-table ('name n) (k v) ...))
#'"FOO"]
[(_ (hash-table (k v) ...))
#'"BAM"]
[(_ (hash-table (k0 v0) (k1 v1) ...))
#'"BAZ"]
[(_ (hash-table x ...))
#'"COMMON"]
[x #'"WHY"]))
(define (define-person . fields)
(for/hash ([k '(name e-mail nickname)]
[field fields])
(values k field)))
(define person (define-person 'my-name 'my-email 'my-nickname))
; gives why (why nothing before matches ?)
(m person)
; my-email
(m (define-person 'my-name 'my-email 'my-nickname))
; gives my-name
(m (hash
'name 'my-name
'e-mail 'my-email
'nickname 'my-nickname))
; gives my-nickname
(m (hash
'nickname 'my-nickname
'name 'my-name
'e-mail 'my-email))
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/82364adf-fe5d-4c6f-bf33-51852c423371o%40googlegroups.com.