unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Stephen Compall <s11@member.fsf.org>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: string parsing/preparation for latex
Date: Tue, 15 Nov 2005 20:52:55 -0600	[thread overview]
Message-ID: <1132109576.10202.44.camel@nocandy.dyndns.org> (raw)
In-Reply-To: <43793ED4.703@fastmail.fm>


[-- Attachment #1.1: Type: text/plain, Size: 3804 bytes --]

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 #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

      reply	other threads:[~2005-11-16  2:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-07 13:53 string parsing/preparation for latex David Pirotte
2005-11-07 15:49 ` Jose Roberto B. de A. Monteiro
2005-11-08 14:52   ` David Pirotte
2005-11-08 16:35     ` Jose Roberto B. de A. Monteiro
2005-11-08 16:56     ` Stephen Compall
2005-11-08  0:34 ` Jon Wilson
2005-11-08 14:26   ` David Pirotte
2005-11-08 20:42     ` Jon Wilson
2005-11-08 21:08       ` klaus schilling
2005-11-09 13:24         ` Jon Wilson
2005-11-14  3:17           ` Stephen Compall
2005-11-15  1:50             ` Jon Wilson
2005-11-16  2:52               ` Stephen Compall [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1132109576.10202.44.camel@nocandy.dyndns.org \
    --to=s11@member.fsf.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).