Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc