define-syntax

Error during expansion of macro in Chicken Scheme

不想你离开。 提交于 2019-12-25 01:48:41
问题 I'm learning how the macro system in Scheme works and I'm trying to make my code look more JavaScript-y. So I thought I would start with the function macro. This is how I want a function definition to look: (function id (x) x) It should expand to the following: (define (id x) x) So I write a macro as follows: (define-syntax function (lambda (name args . body) `(define (,name ,@args) ,@body))) However when I use it I get the following error (in Chicken Scheme): Error: during expansion of

Capturing Macros in Scheme

我的梦境 提交于 2019-12-19 07:52:19
问题 What's the simplest way to define a capturing macro using define-syntax or define-syntax-rule in Racket? As a concrete example, here's the trivial aif in a CL-style macro system. (defmacro aif (test if-true &optional if-false) `(let ((it ,test)) (if it ,if-true ,if-false))) The idea is that it will be bound to the result of test in the if-true and if-false clauses. The naive transliteration (minus optional alternative) is (define-syntax-rule (aif test if-true if-false) (let ((it test)) (if it

how to create a macro in racket where a list becomes the args of said lambda?

徘徊边缘 提交于 2019-12-12 22:22:47
问题 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

How to control order of Scheme macro expansion?

夙愿已清 提交于 2019-12-11 02:35:57
问题 I'm working with the Racket macro extension syntax-id-rules , that some other Scheme implementations provide under the name identifier-syntax . These let you specify macro expansions that will happen even when the defined identifier isn't in head position. So for example: (define hidden #f) (define-syntax proxy (syntax-id-rules (set!) [(set! proxy v) (set! hidden v)] [proxy hidden])) will set up the identifier proxy to be a proxy for hidden . This is a useless example, but it illustrates the

Scheme Macro for nesting expressions

非 Y 不嫁゛ 提交于 2019-12-07 07:20:35
问题 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??])) 回答1: (define bop list) (define-syntax op (syntax-rules () ((op a b) (bop a b

Scheme Macro for nesting expressions

余生颓废 提交于 2019-12-05 10:38:27
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)