[plt-scheme] Define-syntax and #hash - ugly compiling behavior

From: Jos Koot (jos.koot at telefonica.net)
Date: Mon Sep 14 11:28:20 EDT 2009

You mean the following?

#lang scheme
(define-syntax (my-hash stx)
 (syntax-case stx ()
  ((_ (key . val) ...)
    ((keys (map syntax->datum (syntax->list #'(key ...))))
     (vals (map syntax->datum (syntax->list #'(val ...)))))
    ; make the hash-table at expansion time!
    (datum->syntax #'here (make-immutable-hash (map cons keys vals)))))))

; tests:
(define h (my-hash (a . 1) (b . 2) (c . 3)))
(hash-map h list) ; --> ((a 1) (b 2) (c 3))
(hash-ref h 'a) ; 1

Best wishes, Jos.
  ----- Original Message ----- 
  From: Patrick Steiger 
  To: plt-scheme at list.cs.brown.edu 
  Sent: Monday, September 14, 2009 5:39 AM
  Subject: [plt-scheme] Define-syntax and #hash - ugly compiling behavior

  Hello, all. This is my first post on the list, I'm a CS student and I've been giving Scheme a shot. (liking it a lot, so far).

  Now, given the following code:

  (define-syntax myhash
    (syntax-rules (->)
      [(_ (x -> y) ...)
       #hash((x . y) ...)]))

  When trying to compile it, I get:

  . read: expected `(' or `[' or `{' to start a hash pair

  I was told that this happens because the compiler recognizes #hash as a hash-table long before (in the lexical analysis?) knowing that it is inside a (define-syntax), and therefore doesn't get the semantics of '...' - it just interprets it as illegal syntax.

  This seems like a major wart with the language. What are your opinions?

  Patrick Steiger.

  "Theory is when you know something, but it doesn't work. Practice is
  when something works, but you don't know why.
  Programmers combine theory and practice: Nothing works and they don't
  know why." - Anon 


    For list-related administrative tasks:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090914/998dc77e/attachment.html>

Posted on the users mailing list.