unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Thien-Thi Nguyen <ttn@gnuvola.org>
To: guile-user@gnu.org
Subject: Re: sxml-match bad pattern syntax
Date: Tue, 28 Dec 2021 02:58:56 -0500	[thread overview]
Message-ID: <87a6gldutb.fsf@gnuvola.org> (raw)
In-Reply-To: <Ycq6aHxHC/pOkRxV@tuxteam.de> (tomas@tuxteam.de's message of "Tue, 28 Dec 2021 08:19:04 +0100")


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


() <tomas@tuxteam.de>
() Tue, 28 Dec 2021 08:19:04 +0100

   I /think/ the ellipsis is at a wrong place there. Note that
   I'm coming from "traditional" match, and I just have wrapped
   half of my brain around that (which, BTW, is somewhat painful
   :-)

Haha, i know exactly how you feel.

   So take this with a grain of salt.

   * Phenomenology (aka: I barely know what I'm doing):

   If you replace your ellipses above by ". ,rest", things seem
   to work:

    (define (unbogus x)
      (sxml-match x
        ((a (@ . ,attrs) . ,rest)
         `(a (@ ,@(delete '(shape "rect") attrs)) . ,rest))))

    (unbogus '(a (@ (shape "rect") (href "foo.html")) "kid"))

    => (a (@ (href "foo.html")) "kid")

Cool.  I guest the ". ,rest" is a shortcut for the ellipses.

   * Philosophy (aka blah, blah)

   If those ellipses resemble what match do, I think they are
   wrong there:

     (@ . ,attrs) ...

   would mean zero or more times the shape "(@ . ,attrs)". I
   think this isn't what you want.

Ah, right!  The ellipses are a tail that need to follow a head.
I guess i was confused by the documentation's use of ellipses in
the conventional sense rather than the literal sense:

 (define (album->html x)
   (sxml-match x
     [(album (@ (title ,t)) (catalog (num ,n) (fmt ,f)) ...)
      `(ul (li ,t)
           (li (b ,n) (i ,f)) ...)]))

Anyway, thanks to your help, i've managed to cobble together the
following code, attached here for the benefit of future self:

[-- Attachment #1.2: UNBOGUS --]
[-- Type: text/plain, Size: 559 bytes --]

#!/usr/bin/guile -s
!#
(use-modules
 (ice-9 pretty-print)
 (sxml match))

(define (pp x)
  (pretty-print x)
  (newline))

(define (unbogus x)
  (sxml-match x
    ((a (@ . ,attrs) . ,rest)
     `(a (@ ,@(delete '(shape "rect") attrs)) . ,rest))
    (,otherwise
     (if (string? x)
         x
         `(,(car x) ,(cadr x)
           ,@(map unbogus (cddr x)))))))

(define one '(a (@ (shape "rect") (href "foo.html")) "kid"))
(newline)
(pp one)
(pp (unbogus one))

(define bad `(p (@) "some text and " ,one " and " ,one))
(newline)
(pp bad)
(pp (unbogus bad))

[-- Attachment #1.3: Type: text/plain, Size: 441 bytes --]

Maybe it's idiomatic.  Feedback on how to improve it welcome!

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)               ; (2021) Software Libero
   (pcase (context query)               ;       = Dissenso Etico
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502


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

  reply	other threads:[~2021-12-28  7:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-28  6:21 sxml-match bad pattern syntax Thien-Thi Nguyen
2021-12-28  7:19 ` tomas
2021-12-28  7:58   ` Thien-Thi Nguyen [this message]
2021-12-28  8:28     ` tomas
2021-12-28  9:21       ` Thien-Thi Nguyen
2021-12-28 10:06         ` tomas

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=87a6gldutb.fsf@gnuvola.org \
    --to=ttn@gnuvola.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).