RE: Expansion problem

"Jos Koot" <[email protected]>
Newsgroups gmane.comp.lang.racket.user,gmane.lisp.scheme.plt
Message-ID <00EA4D9ED8B44B109F8388937E51665F@SamsungPC>
Things can be simplified I think.

In addition:
using cond in stead of if,
you don't need begin and
you can include definitions in the bodies
(both in finish-expr ... and in body-expr ...)

Does the following do what you want?
Best wishes, Jos

#lang racket

(define-syntax do*
 (syntax-rules ()
  ((do* ((var init next) ...) (test) body-expr ...)
   (do* ((var init next) ...) (test (void)) body-expr ...))
  ((do* ((var init next) ...) (test finish-expr ...) body-expr ...)
   (let* ((var init) ...)
    (let loop ((var var) ...)
     (cond
      (test finish-expr ...)
      (else body-expr ... (let* ((var next) ...) (loop var ...)))))))))

(do* () (#t)) ; -> void

(do* ((i 1 (+ i 1)) (j i (+ j i)))
     ((> (+ i j) 100)
      (define x (+ i j))
      (list i j x))
     (define x (- j i))
     (printf "~s~n" (list i j x)))

(do* ((i 1 (+ i 1)) (j i (+ j i)) (r (list (list i j (- j i))) (cons (list i j (- j i)) r)))
     ((> (+ i j) 100) (reverse r))
     (define-syntax-rule (p) (list i j (- j i)))
     (printf "~s~n" (p)))

-----Original Message-----
From: racket-users-/[email protected] [mailto:racket-users-/[email protected]] On Behalf Of Jean-Michel HUFFLEN
Sent: 31 May 2018 23:46
To: Philip McGrath
Cc: Racket Users
Subject: Re: [racket-users] Expansion problem

Philip McGrath <philip-HuI0B06iylScIzJqBAPImwC/[email protected]> wrote:

> The bindings of a `let`/`let*`/etc. form can be generated by
> macro-expansion, but the binding pairs are not expressions: a
> transformer/macro binding for the left-hand-side identifier doesn't trigger
> macro expansion. This is a good thing, because otherwise you wouldn't be
> able to let-bind names defined as macros.

    OK, I understand, thank you. I think that this feature is not  
related to racket but should be present in Scheme in general. But that  
not obvious: if I understood, bindings for let*-like forms cannot be  
expanded in the sense that (x y) is expanded if x is a macro name. But  
such expressions can be followed by "...".

    Finally, I rewrote "do*" as follows:

(define-syntax do*
   (syntax-rules ()
     ((do* ((var init step) ...) (test expr-0 ...) body-expr-0 ...)
      (let* ((var init) ...)
        (let loop ((var var) ...)
          (if test
              (do* "ending" expr-0 ...)
              (begin body-expr-0 ... (let* ((var step) ...) (loop var  
...)))))))
     ((do* ((var init step ...) ...) (test expr-0 ...) body-expr-0 ...)
      (do* ((do* "place-step" var init step ...) ...)
           (test expr-0 ...)
        body-expr-0 ...))
     ((do* "ending") (if #f #f))
     ((do* "ending" expr-0) expr-0)
     ((do* "ending" expr-0 ...) (begin expr-0 ...))
     ((do* "place-step" var init) (var init var))
     ((do* "place-step" var init step) (var init step))))

> (...)
>    - I see that you have `(do* "ending")` expand to `(if #f #f)`, which is
>    a syntax error in Racket.
>    - Using strings to dispatch to your additional cases will work, but it
>    would be most idiomatic to use Racket's keywords

    Yes, but in fact, I started with defining "do*" in Racket's R7RS  
mode. Many thanks for you help.

J.-M. H.


----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

-- 
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.