How would I go about in doing a define-syntax-rule
that accepts a list as arguments and a list (or a quote, in case it is a single element) as body of a lambda?
i would like to do something like:
>(define a (lambdarize '(x y z) '(+ x y z)))
#<procedure>
>(a 1 2 3)
6
>(define b (lambdarize '(x) 'x))
#<procedure>
>(b 1)
1
I have played around with define-syntax-rule
and apply
, but since lambda
itself seems to be a macro and not a procedure, i have been stumped at trying to find it...
oh... And I would like a solution that does not use eval
, as it is pernicious...
UPDATE
thanks for the answer, Ryan... that pretty much did it! =) the problem with eval was that it is no good at catching the current scope... this way i can do stuff like
(let ([a 1])
(begin
(defmethod add ((x integer?) (y integer?)) (+ x y a))
(add 1 2)
)
which would fail miserably with eval... this is just an academic example, but i think it is a good test for correctness.
It's not possible to create a function where the formal parameters and body are given as run-time values (S-expressions) without using eval
.
But based on your answer to Greg's comment, you can change your defmethod
macro to avoid this issue. Your current approach takes the variables and body expression (program terms), converts them to run-time values, then wants to reinterpret them as program terms. Instead, you should just combine the program terms into a lambda
-expression in the macro, which you can then pass as a value to the helper function:
(define-syntax-rule (defmethod name ((var predicate) ...) body)
(add-to-generic name
(list predicate ...)
(lambda (var ...) body)))
If you wanted to keep the variable names around (eg for error messages), you can also quote them, but separate from the lambda
-expression:
(define-syntax-rule (defmethod name ((var predicate) ...) body)
(add-to-generic name
(list predicate ...)
'(var ...) ;; for errors, debugging, etc
(lambda (var ...) body)))