Cla*_*diu 5 macros scheme nested define-syntax
是否可以在Scheme中编写一个宏(define-syntax例如),它将采用这样的表达式:
(op a b c d e f g h i j)
Run Code Online (Sandbox Code Playgroud)
并将这样的表达式作为输出?
(op (op (op (op (op (op (op (op (op a b) c) d) e) f) g) h) i) j)
Run Code Online (Sandbox Code Playgroud)
当然,任意长度.考虑到这样的一些模板,我想不出办法来做到这一点:
(define-syntax op
(syntax-rules ()
[(_) 'base-case]
[(v1 v2 ...) 'nested-case??]))
Run Code Online (Sandbox Code Playgroud)
(define bop list)
(define-syntax op
(syntax-rules ()
((op a b) (bop a b))
((op a b c ...) (op (bop a b) c ...))))
Run Code Online (Sandbox Code Playgroud)
例如,(op 1 2 3 4)展开(bop (bop (bop 1 2) 3) 4)并评估为(((1 2) 3) 4).