这个 R5RS 宏是我尝试过的,也是我想做的。球拍或其他实现不喜欢这个宏,正是我想要魔法发生的地方。
(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))))
我也一直在阅读定义语法入门并认为 is-eqv 的实现?会为我指出正确的方向,但似乎它不是在那里定义的宏。
如果在 R5RS 编译时不可能,那么如何使用 R6RS 来完成?