|
|
 |
 |
 |
 |
Scheme Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Can I use macro to create Perl-like statement modifiers?
Hi, Can I use Scheme macro to create Perl-like statement modifiers? A statement modifier controls execution flow at the end of an expression. For example, instead of saying if(condition) {doSomthing()}, we will say doSomthing() if(condition). I was wondering if I could implement in Scheme something similar, such as (expression unless condition). I understand that the following code(du stands for do-unless) works: (define-syntax du (syntax-rules () ((_ expression unless condition) (begin (if (not condition) expression))))) but what if I want to remove the "du"? I tried the following, but got a syntax error as expected: (define-syntax unless (syntax-rules () ((expression _ condition) (begin (if (not conition) expression))))) Any suggestion? Thanks, g9yuayon
On May 23, 2:41 pm, yycs@gmail.com wrote: > but what if I want to remove the "du"? I tried the following, but got > a syntax error as expected: > (define-syntax unless > (syntax-rules () > ((expression _ condition) (begin (if (not conition) > expression)))))
I believe macro always evaluates head first so you have to modify the reader macro to accomplish what you need. On mzscheme, you can use its infix notation (described in 11.2.4) to mimic statement modifier. But the infix notation only affect the head, so you still have to place the condition at the end (which differ from built-in mzscheme condition macro). See below for example. HTH, yinso ; this unless macro differ from the ; built-in mzscheme unless macro: condition at the end (define-syntax unless (syntax-rules () ((_ exp exp2 ... condition) (if (not condition) (begin exp exp2 ...))))) ; prefix form... (unless (display 1) (newline) (display 2) (newline) (= 1 0)) ; prints 1\n2\n ; infix form... ((display 1) (newline) (display 2) (newline) . unless . (= 1 0)) ; prints 1\n2\n
|
 |
 |
 |
 |
|