how to create a macro in racket where a list becomes the args of said lambda? -
how go in doing define-syntax-rule
accepts list arguments , list (or quote, in case single element) body of lambda? like:
>(define (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 define-syntax-rule
, apply
, since lambda
seems macro , not procedure, have been stumped @ trying find it...
oh... , solution not use eval
, pernicious...
update
thanks answer, ryan... pretty did it! =) problem eval no @ catching current scope... way can stuff
(let ([a 1]) (begin (defmethod add ((x integer?) (y integer?)) (+ x y a)) (add 1 2) )
which fail miserably eval... academic example, think test correctness.
it's not possible create function formal parameters , body given run-time values (s-expressions) without using eval
.
but based on answer greg's comment, can change defmethod
macro avoid issue. current approach takes variables , body expression (program terms), converts them run-time values, wants reinterpret them program terms. instead, should combine program terms lambda
-expression in macro, can pass value helper function:
(define-syntax-rule (defmethod name ((var predicate) ...) body) (add-to-generic name (list predicate ...) (lambda (var ...) body)))
if wanted keep variable names around (eg error messages), can quote them, separate lambda
-expression:
(define-syntax-rule (defmethod name ((var predicate) ...) body) (add-to-generic name (list predicate ...) '(var ...) ;; errors, debugging, etc (lambda (var ...) body)))
Comments
Post a Comment