Can a macro be written in Scheme (with define-syntax
, for example) which will take expressions like this:
(op a b c d e f g h i j)
And yield expressions like this as output?
(op (op (op (op (op (op (op (op (op a b) c) d) e) f) g) h) i) j)
Of course, for arbitrary lengths. I can't think of a way to do it, given some template like this:
(define-syntax op
(syntax-rules ()
[(_) 'base-case]
[(v1 v2 ...) 'nested-case??]))
(define bop list)
(define-syntax op
(syntax-rules ()
((op a b) (bop a b))
((op a b c ...) (op (bop a b) c ...))))
For example, (op 1 2 3 4)
expands to (bop (bop (bop 1 2) 3) 4)
and evaluates to (((1 2) 3) 4)
.
The function you want to apply to the arguments should itself be an argument to the macro. Barring that, my solution was the same.
#!r6rs
(import (rnrs base))
(define-syntax claudiu
(syntax-rules ()
((claudiu fun first second)
(fun first second))
((claudiu fun first second rest ...)
(claudiu fun (claudiu fun first second) rest ...))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With