這個R5RS宏是我嘗試過的,幾乎是我想要做的。球拍或其他實現不喜歡這個宏恰好在我想要發生魔法的地方。是否可以匹配R5RS宏定義中的重複模式define-syntax/syntax-rules?
(define-syntax quote-unique
(syntax-rules (magic end)
;; end case
((quote-unique magic processed end)
'processed)
;; finished iteration
((quote-unique magic (processed ...) sym1 end rest ...)
(quote-unique magic (processed ... sym1) rest ... end))
;; match (doesn't work since racket doesn't like sym1 twice in template)
;; but I'm looking for the same expression twice
((quote-unique magic processed sym1 sym1 . rest)
(quote-unique magic processed sym1 . rest))
;; rotate
((quote-unique magic processed sym1 sym2 rest ...)
(quote-unique magic processed sym1 rest ... sym2))
;; start iteration
((quote-unique rest ...)
(quote-unique magic() rest ... end))))
這Common Lisp中是很容易:
(defmacro quote-unique (&rest xs)
(labels ((remove-duplicates (lis)
(if lis
(if (member (car lis) (cdr lis))
(remove-duplicates (cdr lis))
(cons (car lis) (remove-duplicates (cdr lis)))))))
(list 'quote (remove-duplicates xs))))
我也一直在讀Define syntax primer和思考的實施,當量?會讓我指出正確的方向,但它似乎並不是在那裏定義的宏觀。
如果在R5RS編譯時不可能,那麼R6RS怎麼能這樣做呢?