chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] unbound variable: or


From: Michele La Monaca
Subject: Re: [Chicken-users] unbound variable: or
Date: Sat, 30 May 2015 15:42:41 +0200

On Sat, May 30, 2015 at 4:23 AM, John Cowan <address@hidden> wrote:
> Jinsong Liang scripsit:
>
>> I want to learn some basic macro programming in Chicken. However, it seems
>> there are multiple macro definition APIs in Chicken: define-syntax,
>> syntax-rules, syntax-case, define-macro. Which one should I start with?
>
> Define-macro is completely obsolete and not supported in Chicken 4 or
> any modern Scheme.

Gambit has it. Too bad Chicken 4 dropped it.

I don’t think describing define-macro "obsolete” is 100% correct.
Let’s say someone had the (bad?) idea to masquerade/embed it in other
more baroque forms. For example in Chicken you have to write:

(define-syntax apply-any
  (er-macro-transformer
    (lambda (form rename compare)
      (let ((x (cadr form)) (lst (caddr form)))
        (eval `(cons ',x ,lst))))))

instead of:

(define-macro apply-any
 (lambda (x lst)
   (eval `(cons ‘,x ,lst))))

Define-macro is the most basic, no-frills macro system and the one
which will give you the most profound understanding of what a macro is
and is not. Oh, and it is quite powerful. I’ve used Gambit to
practice. Only when you dominate it, I suggest to move on to
syntax-rules. The latter works at a higher layer (see below), enforces
hygiene and introduces you to pattern matching (which is not a macro
specific topic).

(define-syntax apply-any
  (syntax-rules ()
    ((_ x lst) (eval (cons 'x lst)))))

(define l '(#f (+ 1 2) (print "hello")))

(apply-any or l)

=> 3

(apply-any + (list 1 2 3))

=> 6

(let ((x 9)) (apply-any or '(#f (+ x 1) (print "hello"))))

Error: unbound variable: x

Oops.

I’m definitely not an expert of the topic so take my suggestions for
what you think they are worth.

Regards,
Michele



reply via email to

[Prev in Thread] Current Thread [Next in Thread]