Wrong result from a define-syntax with pattern maching on a hash table

39 views
Skip to first unread message

Oualmakran Yassine

unread,
Jul 6, 2020, 6:47:24 AM7/6/20
to Racket Users
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))


Sorawee Porncharoenwase

unread,
Jul 6, 2020, 7:04:27 AM7/6/20
to Oualmakran Yassine, Racket Users
What's wrong with `match`? It seems to be what you want. Macro is not going to help you here.



--
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 racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/82364adf-fe5d-4c6f-bf33-51852c423371o%40googlegroups.com.

Oualmakran Yassine

unread,
Jul 6, 2020, 7:23:34 AM7/6/20
to Racket Users
In fact, I would like to have a macro that will generate some text from a list of hash tables for scribble. So this macro is just a first step to try to achieve that (it's also the first time I try a non toy program in any Lisp).
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.

Sorawee Porncharoenwase

unread,
Jul 6, 2020, 7:44:14 AM7/6/20
to Oualmakran Yassine, Racket Users
I still don't understand why you need to define a macro for that. We write macros to manipulate _code_ by transforming a syntax object into another syntax object. Extracting a value from a hash table or generating a text from hash tables has nothing to do with syntax objects. And it can be done with basic programming.

Can you tell what you are trying to do with more details, perhaps?

To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/946b66b0-71b0-44e4-abef-2b577d564dc7o%40googlegroups.com.

Oualmakran Yassine

unread,
Jul 6, 2020, 8:00:58 AM7/6/20
to Racket Users
 Sure.

I would like to create a document with a repetitive structure in scribble.

Let say I have something like this:
@section{First section}
Some text ...
@subsection{First subsection}
@itemlist...

@section{Second section}
Some text ...
@subsection{Second subsection}
@itemlist...

etc.

I would like to know how is it possible to have this without writing multiple section, subsection, etc. every time (the purpose is more to understand how the language works than anything else).

Sorawee Porncharoenwase

unread,
Jul 6, 2020, 8:09:56 AM7/6/20
to Oualmakran Yassine, Racket Users
#lang scribble/manual

@(require (only-in racket ~a))

@(define persons (list (hash
                        'name 'name1
                        'e-mail 'email1
                        'nickname 'nickname1)
                       (hash
                        'name 'name2
                        'e-mail 'email2
                        'nickname 'nickname2)
                       (hash
                        'name 'name3
                        'e-mail 'email3
                        'nickname 'nickname3)))

@(define (generate-section person)
   (list @section{Here's a section for @(~a (hash-ref person 'name))}
         @subsection{My information}
         @itemlist[
           @item{Email: @(~a (hash-ref person 'e-mail))}
           @item{Nickname: @(~a (hash-ref person 'nickname))}
         ]))

@(map generate-section persons)

generates this document:

Screen Shot 2020-07-06 at 05.09.08.png

--
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 racket-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages