I'm in a process of implementing Hygienic macros in my Scheme implementation, I've just implemented syntax-rules
, but I have this code:
(define odd?
(syntax-rules ()
((_ x) (not (even? x)))))
what should be the difference between that and this:
(define-syntax odd?
(syntax-rules ()
((_ x) (not (even? x)))))
from what I understand syntax-rules
just return syntax transformer, why you can't just use define
to assign that to symbol? Why I need to use define-syntax
? What extra stuff that expression do?
Should first also work in scheme? Or only the second one?
Also what is the difference between let
vs let-syntax
and letrec
vs letrec-syntax
. Should (define|let|letrec)-syntax
just typecheck if the value is syntax transformer?
EDIT:
I have this implementation, still using lisp macros:
;; -----------------------------------------------------------------------------
(define-macro (let-syntax vars . body)
`(let ,vars
,@(map (lambda (rule)
`(typecheck "let-syntax" ,(car rule) "syntax"))
vars)
,@body))
;; -----------------------------------------------------------------------------
(define-macro (letrec-syntax vars . body)
`(letrec ,vars
,@(map (lambda (rule)
`(typecheck "letrec-syntax" ,(car rule) "syntax"))
vars)
,@body))
;; -----------------------------------------------------------------------------
(define-macro (define-syntax name expr)
(let ((expr-name (gensym)))
`(define ,name
(let ((,expr-name ,expr))
(typecheck "define-syntax" ,expr-name "syntax")
,expr-name))))
This this code correct?
Should this code works?
(let ((let (lambda (x) x)))
(let-syntax ((odd? (syntax-rules ()
((_ x) (not (even? x))))))
(odd? 11)))