guile-user
[Top][All Lists]
Advanced

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

Re: string parsing/preparation for latex


From: Stephen Compall
Subject: Re: string parsing/preparation for latex
Date: Tue, 15 Nov 2005 20:52:55 -0600

On Mon, 2005-11-14 at 19:50 -0600, Jon Wilson wrote: 
> Yeah, I guess you're right.  Like I said, I'm new to the whole idea of
> macros, and I'm kind of looking for a really good use-case for the
> things.  Can you suggest a non-contrived situation in which a macro
> would be the best solution?

WARNING: All this is wildly untested, debugging is left as an exercise
for those more patient

Implicit begin when you don't care about the else case (from R5RS spec):

(define-syntax when
  (syntax-rules ()
    ((_ test when-form wf2 ...)
     (if test (begin when-form wf2 ...)))))

Example: (when (some-test)
           (do-this) (and-do-that) and-return-this)

A list collector:

(use-modules (srfi srfi-1))

(define-macro (with-collect setters . body-forms)
  ;;note: use make-symbol instead of gensym in 1.7
  (let ((syms (map (lambda (ign) (gensym)) setters)))
    `(let ,(map (lambda (sym) (list sym ''())) syms)
       (let ,(map (lambda (sym setter)
                    (list setter `(lambda (elt)
                                    (set! ,sym (cons elt ,sym)))))
                  syms setters)
         ,@body-forms
         (values ,@(map (lambda (sym) (list 'reverse! sym)) syms))))))

Example: (with-collect (c) (c 1) (c 3)) => (1 3)

As you can see, with-collect could be implemented as the function
call-with-collect, by instead of passing body forms you pass a 1-arg
closure that receives the setter function.  However, the above supports
multiple collects, whereas a function would have to receive another
argument telling it how many collecting functions to pass.  Your
decision, of course, depends on whether you think supporting more
collectors is important.

The problem with-collect fixes directly is the (set! var (cons elt
var)) ... (reverse! var) pattern.  This is called push/nreverse in Lisp,
the former of which you can easily implement with a macro:

(define-macro (push elt place)
  (if (pair? place)             ;using generalized set?
      (let ((place-vars (map (lambda (ign) (gensym)) place)))
        `(let ,(map list place-vars place)
           (set! ,place-vars (cons ,elt ,place-vars))))
      `(set! ,place (cons ,elt ,place))))

Example: (let ((l '())) (push 1 l) (push 2 l) l) => (2 1)

Also in with-collect, I used a common pattern for map several times.
Perhaps that wants a macro, with a set of predefined names or something,
exploiting the common occurrence of inline lambda forms containing just
one body form.

cond with each clause unwrapped:

(define-macro (flat-cond . clauses)
  (cons 'cond
        (with-collect (add-cond-form)
          (let lp ((whats-left clauses))
            (cond ((null? whats-left) #f)
                  ((and (pair? whats-left) (pair? (rest whats-left)))
                   (add-cond-form
                    (list (first whats-left) (second whats-left)))
                   (lp (cddr whats-left)))
                  (else (scm-error 'misc-error "flat-cond"
                                   "bad cond clause ~S in ~S"
                                   (list whats-left clauses) #f)))))))

Example: (flat-cond (null? whats-left) #f
                    (pair? whats-left) (add-cond-form something)
                    else (error "bad flat-cond clause"))

I suppose one rubric for macros might be whether it improves clarity of
expression, if you share that goal in choosing Scheme as a programming
language.  map is great, but maybe a macro version with an implicit
lambda-form and lambda list might be very helpful in the definition of
with-collect, in terms of clarity.  Also, while you could implement
flat-cond as a function, that would ultimately get in your way.

-- 
Stephen Compall
http://scompall.nocandysoftware.com/blog

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

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