unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Syntax-Case macro that selects the N-th element from a list
@ 2021-04-05 11:30 Dr. Arne Babenhauserheide
  2021-04-05 12:21 ` Linus Björnstam
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-04-05 11:30 UTC (permalink / raw)
  To: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 3860 bytes --]

Hi,

In dryads-wake I need selection of the element in a list in a macro from
user-input. Currently I have multiple macros, and the correct one (which
strips the non-selected choices) is selected in a simple cond:

(define-syntax-rule (Choose resp . choices)
   "Ask questions, apply consequences"
   (cond
    ((equal? resp 1) ;; resp is user-input. It is a natural number.
     (Respond1 choices))
    ((equal? resp 2)
     (Respond2 choices))
    ((equal? resp 3)
     (Respond3 choices))
    (else
     #f)))

For this however I have three syntax-case macros:

(define-syntax Respond1
  (lambda (x)
    (syntax-case x ()
      ((_ ((question consequences ...) choices ...))
        #`(begin
           (respond consequences ...)))
      ((_ (choices ...))
        #`(begin #f)))))

(define-syntax Respond2
  (lambda (x)
    (syntax-case x ()
      ((_ (choice choices ...))
        #`(begin
           (Respond1 (choices ...))))
      ((_ (choices ...))
        #`(begin #f)))))

(define-syntax Respond3
  (lambda (x)
    (syntax-case x ()
      ((_ (a b choices ...))
        #`(Respond1 (choices ...)))
      ((_ (choices ...))
        #`(begin #f)))))


I would like to get rid of those three definitions and replace them by
at most two (one that strips N initial list entries, and Respond1).

I cannot move to procedures, because I have code that must be executed
only during final processing, and when I evaluate any of the
consequences (as it happens with procedure-arguments), then the timing
of the code execution does not match anymore. So I must absolutely do
this in macros.


I’ve tried to get that working, but all my tries failed. Is there a way
and can you show it to me?

This is a minimal working example. The output should stay the same,
except for part 4, which needs this change to work (see at the bottom),
but I would like to:

- replace Respond2 and Respond3 by something recursive, so resp can have
  arbitrary high values (not infinite: max the length of the options) and
- replace the cond-clause by a call to the recursive macro.

(define-syntax-rule (respond consequence consequence2 ...)
  (begin
    (write consequence)
    (when (not (null? '(consequence2 ...)))
      (write (car (cdr (car `(consequence2 ...))))))))

(define-syntax Respond1
  (lambda (x)
    (syntax-case x ()
      ((_ ((question consequences ...) choices ...))
        #`(begin
           (respond consequences ...)))
      ((_ (choices ...))
        #`(begin #f)))))

(define-syntax Respond2
  (lambda (x)
    (syntax-case x ()
      ((_ (choice choices ...))
        #`(begin
           (Respond1 (choices ...))))
      ((_ (choices ...))
        #`(begin #f)))))

(define-syntax Respond3
  (lambda (x)
    (syntax-case x ()
      ((_ (a b choices ...))
        #`(Respond1 (choices ...)))
      ((_ (choices ...))
        #`(begin #f)))))


(define-syntax-rule (Choose resp . choices)
   "Ask questions, apply consequences"
   (cond
    ((equal? resp 1)
     (Respond1 choices))
    ((equal? resp 2)
     (Respond2 choices))
    ((equal? resp 3)
     (Respond3 choices))
    (else
     #f)))


(display "Choose 1: should be bar:")
(Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
(newline)
(display "Choose 2: should be warhar:")
(Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
(newline)
(display "Choose 3: should be mar:")
(Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
(newline)
(display "Choose 4: should be tar:")
(Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
(newline)
(display "Choose 5: should be #f:")
(Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
(newline)


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Syntax-Case macro that selects the N-th element from a list
  2021-04-05 11:30 Syntax-Case macro that selects the N-th element from a list Dr. Arne Babenhauserheide
@ 2021-04-05 12:21 ` Linus Björnstam
  2021-04-05 13:40   ` Linus Björnstam
  2021-04-05 13:51 ` Tim Van den Langenbergh
  2021-04-05 15:08 ` Taylan Kammer
  2 siblings, 1 reply; 6+ messages in thread
From: Linus Björnstam @ 2021-04-05 12:21 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide, Guile User Mailing List

Can you use the procedural part of syntax-rules? You have the power of using scheme at expansion time, which means you could do list-ref all you want.

The only thing is that guile lacks syntax->list, so sometimes you have to manually turn it into a list. Say you are matching ((_ stuff ...) Body) stuff is a syntax object. You could turn it into a list of syntax objects by doing  #'(stuff ...). Then you can treat it as a regular list, and use quasisyntax to put it back into your output syntax. 

Writing this on my phone. Sorry for the brevity (and lack of code).

-- 
  Linus Björnstam

On Mon, 5 Apr 2021, at 13:30, Dr. Arne Babenhauserheide wrote:
> Hi,
> 
> In dryads-wake I need selection of the element in a list in a macro from
> user-input. Currently I have multiple macros, and the correct one (which
> strips the non-selected choices) is selected in a simple cond:
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1) ;; resp is user-input. It is a natural number.
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> For this however I have three syntax-case macros:
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> I would like to get rid of those three definitions and replace them by
> at most two (one that strips N initial list entries, and Respond1).
> 
> I cannot move to procedures, because I have code that must be executed
> only during final processing, and when I evaluate any of the
> consequences (as it happens with procedure-arguments), then the timing
> of the code execution does not match anymore. So I must absolutely do
> this in macros.
> 
> 
> I’ve tried to get that working, but all my tries failed. Is there a way
> and can you show it to me?
> 
> This is a minimal working example. The output should stay the same,
> except for part 4, which needs this change to work (see at the bottom),
> but I would like to:
> 
> - replace Respond2 and Respond3 by something recursive, so resp can have
>   arbitrary high values (not infinite: max the length of the options) and
> - replace the cond-clause by a call to the recursive macro.
> 
> (define-syntax-rule (respond consequence consequence2 ...)
>   (begin
>     (write consequence)
>     (when (not (null? '(consequence2 ...)))
>       (write (car (cdr (car `(consequence2 ...))))))))
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1)
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> 
> (display "Choose 1: should be bar:")
> (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 2: should be warhar:")
> (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 3: should be mar:")
> (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 4: should be tar:")
> (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 5: should be #f:")
> (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> 
> 
> Best wishes,
> Arne
> -- 
> Unpolitisch sein
> heißt politisch sein
> ohne es zu merken
> 
> Attachments:
> * signature.asc



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Syntax-Case macro that selects the N-th element from a list
  2021-04-05 12:21 ` Linus Björnstam
@ 2021-04-05 13:40   ` Linus Björnstam
  2021-04-05 16:24     ` Dr. Arne Babenhauserheide
  0 siblings, 1 reply; 6+ messages in thread
From: Linus Björnstam @ 2021-04-05 13:40 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide, Guile User Mailing List

That "syntax-rules" is of course syntax-case.

Try writing it first with unhygienic macros and get that working before porting to syntax-case if you don't know the ins-and-outs of syntax-case.

-- 
  Linus Björnstam

On Mon, 5 Apr 2021, at 14:21, Linus Björnstam wrote:
> Can you use the procedural part of syntax-rules? You have the power of 
> using scheme at expansion time, which means you could do list-ref all 
> you want.
> 
> The only thing is that guile lacks syntax->list, so sometimes you have 
> to manually turn it into a list. Say you are matching ((_ stuff ...) 
> Body) stuff is a syntax object. You could turn it into a list of syntax 
> objects by doing  #'(stuff ...). Then you can treat it as a regular 
> list, and use quasisyntax to put it back into your output syntax. 
> 
> Writing this on my phone. Sorry for the brevity (and lack of code).
> 
> -- 
>   Linus Björnstam
> 
> On Mon, 5 Apr 2021, at 13:30, Dr. Arne Babenhauserheide wrote:
> > Hi,
> > 
> > In dryads-wake I need selection of the element in a list in a macro from
> > user-input. Currently I have multiple macros, and the correct one (which
> > strips the non-selected choices) is selected in a simple cond:
> > 
> > (define-syntax-rule (Choose resp . choices)
> >    "Ask questions, apply consequences"
> >    (cond
> >     ((equal? resp 1) ;; resp is user-input. It is a natural number.
> >      (Respond1 choices))
> >     ((equal? resp 2)
> >      (Respond2 choices))
> >     ((equal? resp 3)
> >      (Respond3 choices))
> >     (else
> >      #f)))
> > 
> > For this however I have three syntax-case macros:
> > 
> > (define-syntax Respond1
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ ((question consequences ...) choices ...))
> >         #`(begin
> >            (respond consequences ...)))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > (define-syntax Respond2
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ (choice choices ...))
> >         #`(begin
> >            (Respond1 (choices ...))))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > (define-syntax Respond3
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ (a b choices ...))
> >         #`(Respond1 (choices ...)))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > 
> > I would like to get rid of those three definitions and replace them by
> > at most two (one that strips N initial list entries, and Respond1).
> > 
> > I cannot move to procedures, because I have code that must be executed
> > only during final processing, and when I evaluate any of the
> > consequences (as it happens with procedure-arguments), then the timing
> > of the code execution does not match anymore. So I must absolutely do
> > this in macros.
> > 
> > 
> > I’ve tried to get that working, but all my tries failed. Is there a way
> > and can you show it to me?
> > 
> > This is a minimal working example. The output should stay the same,
> > except for part 4, which needs this change to work (see at the bottom),
> > but I would like to:
> > 
> > - replace Respond2 and Respond3 by something recursive, so resp can have
> >   arbitrary high values (not infinite: max the length of the options) and
> > - replace the cond-clause by a call to the recursive macro.
> > 
> > (define-syntax-rule (respond consequence consequence2 ...)
> >   (begin
> >     (write consequence)
> >     (when (not (null? '(consequence2 ...)))
> >       (write (car (cdr (car `(consequence2 ...))))))))
> > 
> > (define-syntax Respond1
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ ((question consequences ...) choices ...))
> >         #`(begin
> >            (respond consequences ...)))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > (define-syntax Respond2
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ (choice choices ...))
> >         #`(begin
> >            (Respond1 (choices ...))))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > (define-syntax Respond3
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_ (a b choices ...))
> >         #`(Respond1 (choices ...)))
> >       ((_ (choices ...))
> >         #`(begin #f)))))
> > 
> > 
> > (define-syntax-rule (Choose resp . choices)
> >    "Ask questions, apply consequences"
> >    (cond
> >     ((equal? resp 1)
> >      (Respond1 choices))
> >     ((equal? resp 2)
> >      (Respond2 choices))
> >     ((equal? resp 3)
> >      (Respond3 choices))
> >     (else
> >      #f)))
> > 
> > 
> > (display "Choose 1: should be bar:")
> > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> > (newline)
> > (display "Choose 2: should be warhar:")
> > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> > (newline)
> > (display "Choose 3: should be mar:")
> > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> > (newline)
> > (display "Choose 4: should be tar:")
> > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> > (newline)
> > (display "Choose 5: should be #f:")
> > (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> > (newline)
> > 
> > 
> > Best wishes,
> > Arne
> > -- 
> > Unpolitisch sein
> > heißt politisch sein
> > ohne es zu merken
> > 
> > Attachments:
> > * signature.asc
> 
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Syntax-Case macro that selects the N-th element from a list
  2021-04-05 11:30 Syntax-Case macro that selects the N-th element from a list Dr. Arne Babenhauserheide
  2021-04-05 12:21 ` Linus Björnstam
@ 2021-04-05 13:51 ` Tim Van den Langenbergh
  2021-04-05 15:08 ` Taylan Kammer
  2 siblings, 0 replies; 6+ messages in thread
From: Tim Van den Langenbergh @ 2021-04-05 13:51 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide; +Cc: guile-user

On Monday, 5 April 2021 13:30:21 CEST you wrote:
> Hi,
> 
> In dryads-wake I need selection of the element in a list in a macro from
> user-input. Currently I have multiple macros, and the correct one (which
> strips the non-selected choices) is selected in a simple cond:
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1) ;; resp is user-input. It is a natural number.
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> For this however I have three syntax-case macros:
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> I would like to get rid of those three definitions and replace them by
> at most two (one that strips N initial list entries, and Respond1).
> 
> I cannot move to procedures, because I have code that must be executed
> only during final processing, and when I evaluate any of the
> consequences (as it happens with procedure-arguments), then the timing
> of the code execution does not match anymore. So I must absolutely do
> this in macros.
> 
> 
> I’ve tried to get that working, but all my tries failed. Is there a way
> and can you show it to me?
> 
> This is a minimal working example. The output should stay the same,
> except for part 4, which needs this change to work (see at the bottom),
> but I would like to:
> 
> - replace Respond2 and Respond3 by something recursive, so resp can have
>   arbitrary high values (not infinite: max the length of the options) and
> - replace the cond-clause by a call to the recursive macro.
> 
> (define-syntax-rule (respond consequence consequence2 ...)
>   (begin
>     (write consequence)
>     (when (not (null? '(consequence2 ...)))
>       (write (car (cdr (car `(consequence2 ...))))))))
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1)
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> 
> (display "Choose 1: should be bar:")
> (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 2: should be warhar:")
> (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 3: should be mar:")
> (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 4: should be tar:")
> (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 5: should be #f:")
> (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> 
> 
> Best wishes,
> Arne

Hello, Dr. Arne,

would simply transforming the question . response pairs to a list of lists of 
responses work?

E.G.

#+begin_src scheme
(define-syntax questions->responses
  (syntax-rules ()
	((_ (question response ...) choices ...)
	 (cons (list response ...)
		   (questions->responses choices ...)))
	((_ choices ...)
	 '())))
#+end_src

Vale,

-Tim





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Syntax-Case macro that selects the N-th element from a list
  2021-04-05 11:30 Syntax-Case macro that selects the N-th element from a list Dr. Arne Babenhauserheide
  2021-04-05 12:21 ` Linus Björnstam
  2021-04-05 13:51 ` Tim Van den Langenbergh
@ 2021-04-05 15:08 ` Taylan Kammer
  2 siblings, 0 replies; 6+ messages in thread
From: Taylan Kammer @ 2021-04-05 15:08 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide, Guile User Mailing List

On 05.04.2021 13:30, Dr. Arne Babenhauserheide wrote:
> Hi,
> 
> In dryads-wake I need selection of the element in a list in a macro from
> user-input. Currently I have multiple macros, and the correct one (which
> strips the non-selected choices) is selected in a simple cond:
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1) ;; resp is user-input. It is a natural number.
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> For this however I have three syntax-case macros:
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> I would like to get rid of those three definitions and replace them by
> at most two (one that strips N initial list entries, and Respond1).
> 
> I cannot move to procedures, because I have code that must be executed
> only during final processing, and when I evaluate any of the
> consequences (as it happens with procedure-arguments), then the timing
> of the code execution does not match anymore. So I must absolutely do
> this in macros.
> 
> 
> I’ve tried to get that working, but all my tries failed. Is there a way
> and can you show it to me?
> 
> This is a minimal working example. The output should stay the same,
> except for part 4, which needs this change to work (see at the bottom),
> but I would like to:
> 
> - replace Respond2 and Respond3 by something recursive, so resp can have
>   arbitrary high values (not infinite: max the length of the options) and
> - replace the cond-clause by a call to the recursive macro.
> 
> (define-syntax-rule (respond consequence consequence2 ...)
>   (begin
>     (write consequence)
>     (when (not (null? '(consequence2 ...)))
>       (write (car (cdr (car `(consequence2 ...))))))))
> 
> (define-syntax Respond1
>   (lambda (x)
>     (syntax-case x ()
>       ((_ ((question consequences ...) choices ...))
>         #`(begin
>            (respond consequences ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond2
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (choice choices ...))
>         #`(begin
>            (Respond1 (choices ...))))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> (define-syntax Respond3
>   (lambda (x)
>     (syntax-case x ()
>       ((_ (a b choices ...))
>         #`(Respond1 (choices ...)))
>       ((_ (choices ...))
>         #`(begin #f)))))
> 
> 
> (define-syntax-rule (Choose resp . choices)
>    "Ask questions, apply consequences"
>    (cond
>     ((equal? resp 1)
>      (Respond1 choices))
>     ((equal? resp 2)
>      (Respond2 choices))
>     ((equal? resp 3)
>      (Respond3 choices))
>     (else
>      #f)))
> 
> 
> (display "Choose 1: should be bar:")
> (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 2: should be warhar:")
> (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 3: should be mar:")
> (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 4: should be tar:")
> (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> (display "Choose 5: should be #f:")
> (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar))
> (newline)
> 
> 
> Best wishes,
> Arne
> 

Is there a reason you want to separate 'choose' from the various
'respondN' macros?  That seems superfluous to me.

And if the number of choices is limited, you don't even need to use
procedural macros.  The following works:

;; Note: I like using the notation <foo> for template variables.
;; Feels highly intuitive to me.

(define-syntax choose
  (syntax-rules ()
    ((_ 1 <c1> <rest> ...)
     (respond <c1>))
    ((_ 2 <c1> <c2> <r> ...)
     (respond <c2>))
    ((_ 3 <c1> <c2> <c3> <r> ...)
     (respond <c3>))
    ((_ 4 <c1> <c2> <c3> <c4> <r> ...)
     (respond <c4>))
    ((_ 5 <c1> <c2> <c3> <c4> <c5> <r> ...)
     (respond <c5>))
    ((_ <r> ...) #f)))

You could also leave out the final clause and thus let it be an implicit
syntax error, or explicitly invoke 'syntax-error' with an explanation.

But if you want to support an arbitrary numbers of choices, the
following procedural macro will do it:

(define-syntax choose
  (lambda (stx)
    (syntax-case stx ()
      ((_ <n> . <choices>)
       (let* ((n (syntax->datum #'<n>))
              (choice-list (syntax->datum #'<choices>))
              (choice-datum (list-ref choice-list (- n 1)))
              (choice-stx (datum->syntax #'<choices> choice-datum)))
         #`(respond #,choice-stx))))))

I've written a very trivial explanation of syntax-case usage here, in
case anyone struggles as I did at first:

http://taylanub.github.io/doc/syntax-case-example.scm.txt


Hope that helps!

- Taylan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Syntax-Case macro that selects the N-th element from a list
  2021-04-05 13:40   ` Linus Björnstam
@ 2021-04-05 16:24     ` Dr. Arne Babenhauserheide
  0 siblings, 0 replies; 6+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-04-05 16:24 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 1000 bytes --]

Hi Linus,

thank you for your tipps!

Linus Björnstam writes:

> Can you use the procedural part of syntax-rules? You have the power of 
> using scheme at expansion time, which means you could do list-ref all 
> you want.
> 
> That "syntax-rules" is of course syntax-case.
> 
> The only thing is that guile lacks syntax->list, so sometimes you have 
> to manually turn it into a list. Say you are matching ((_ stuff ...) 
> Body) stuff is a syntax object. You could turn it into a list of syntax 
> objects by doing  #'(stuff ...). Then you can treat it as a regular 
> list, and use quasisyntax to put it back into your output syntax. 
> Try writing it first with unhygienic macros and get that working
> before porting to syntax-case if you don't know the ins-and-outs of
> syntax-case.

I have not yet written an unhygienic macro in Guile.

Do I use the internal macros for that?

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-04-05 16:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-05 11:30 Syntax-Case macro that selects the N-th element from a list Dr. Arne Babenhauserheide
2021-04-05 12:21 ` Linus Björnstam
2021-04-05 13:40   ` Linus Björnstam
2021-04-05 16:24     ` Dr. Arne Babenhauserheide
2021-04-05 13:51 ` Tim Van den Langenbergh
2021-04-05 15:08 ` Taylan Kammer

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).