Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: CL vs scheme macros, namespaces.

22 views
Skip to first unread message

WJ

unread,
Mar 25, 2015, 4:53:59 AM3/25/15
to
Pascal Bourguignon wrote:

> To take a simple example:
>
> (define-syntax -->
> (syntax-rules ()
> ((_ . original)
> (-->helper () original))))
>
> (define-syntax -->helper
> (syntax-rules ()
> ((_ pairs (x0 y0 . more))
> (-->helper ((x0 y0) . pairs) more))
> ((_ pairs body)
> (let pairs . body))))
>
>
>
> (defmacro --> (&rest [var-val]*-body)
> `(let ,(loop for (var val) on (butlast [var-val]*-body) by (function cddr)
> collect (list var val))
> ,@(last [var-val]*-body)))

The helper macro isn't needed.

Scheme (Gauche and Racket):

The goal is a let-macro that doesn't need parentheses
around the bindings. Only one expression is allowed
after the bindings.

;; Pattern-matching is powerful and fun.
(define-syntax -->
(syntax-rules ()
( [_ pairs body] (let pairs body))
( [_ (k-v ...) k v more ...] (--> (k-v ... (k v)) more ...))
( [_ more ...] (--> () more ...))))


gosh> (--> a 2 m 44 z 88 (print (list a m z)))
(2 44 88)

You can even give the macro a leg up if you wish:

gosh> (--> () a 2 m 44 z 88 (print (list a m z)))
(2 44 88)
gosh> (--> ((q 500)) a 2 m 44 z 88 (print (list a m z q)))
(2 44 88 500)

You can even specify the let-bindings in the usual way:

gosh> (--> ((a 2) (m 44) (z 88)) (print (list a m z)))
(2 44 88)

WJ

unread,
Mar 25, 2015, 5:17:08 AM3/25/15
to
With some error checking:

(define-syntax -->
(syntax-rules ()
( [_ pairs body] (let pairs body))
( [_ (k-v ...) k v more ...] (--> (k-v ... (k v)) more ...))
( [_ thing] (syntax-error "Error in macro -->."))
( [_ more ...] (--> () more ...))))

gosh> (lambda () (--> a 2 b (list a b)) )
*** ERROR: Compile Error: Error in macro -->.


0 new messages