From: ludo@gnu.org (Ludovic Courtès)
To: Alex Shinn <alexshinn@gmail.com>
Cc: guile-devel@gnu.org
Subject: Re: ‘match’ and “k or more” patterns
Date: Sun, 19 Sep 2010 23:30:27 +0200 [thread overview]
Message-ID: <87wrqhjua4.fsf@gnu.org> (raw)
In-Reply-To: <AANLkTikKbVssXkdfc=DnKgEJr2xJxsNrbjL1vifqkYV2@mail.gmail.com> (Alex Shinn's message of "Wed, 8 Sep 2010 11:18:18 +0900")
[-- Attachment #1.1: Type: text/plain, Size: 729 bytes --]
Hi Alex,
Alex Shinn <alexshinn@gmail.com> writes:
> On Mon, Sep 6, 2010 at 9:12 PM, Ludovic Courtès <ludo@gnu.org> wrote:
[...]
>> I do! :-)
>>
>> http://git.sv.gnu.org/cgit/guile-rpc.git/tree/modules/rpc/compiler.scm#n312
>>
>> Well it uses only ‘..1’. The same code would work with ‘..1’ replaced
>> by ‘...’, but then errors in the input wouldn’t be detected as nicely.
>
> "..1" is actually useful
The attached patch adds support for ‘..1’. I’ll apply it to Guile if
you’re OK with applying it upstream.
What do you think?
BTW, I had fearfully avoided to hack a pattern matcher until now and I
was pleased to see how tractable this code is!
Thanks,
Ludo’.
[-- Attachment #1.2: Type: application/pgp-signature, Size: 197 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 1941 bytes --]
diff --git a/module/ice-9/match.upstream.scm b/module/ice-9/match.upstream.scm
index 963b89f..bf3335b 100644
--- a/module/ice-9/match.upstream.scm
+++ b/module/ice-9/match.upstream.scm
@@ -125,7 +125,7 @@
;; pattern so far.
(define-syntax match-two
- (syntax-rules (_ ___ *** quote quasiquote ? $ = and or not set! get!)
+ (syntax-rules (_ ___ ..1 *** quote quasiquote ? $ = and or not set! get!)
((match-two v () g+s (sk ...) fk i)
(if (null? v) (sk ... i) fk))
((match-two v (quote p) g+s (sk ...) fk i)
@@ -161,6 +161,10 @@
(match-extract-vars p (match-gen-search v p q g+s sk fk i) i ()))
((match-two v (p *** . q) g+s sk fk i)
(match-syntax-error "invalid use of ***" (p *** . q)))
+ ((match-two v (p ..1) g+s sk fk i)
+ (if (pair? v)
+ (match-one v (p ___) g+s sk fk i)
+ fk))
((match-two v (p . q) g+s sk fk i)
(if (pair? v)
(let ((w (car v)) (x (cdr v)))
diff --git a/test-suite/tests/match.test b/test-suite/tests/match.test
index 70a15ec..d1432d8 100644
--- a/test-suite/tests/match.test
+++ b/test-suite/tests/match.test
@@ -67,6 +67,16 @@
((x . rest)
(and (eq? x 'a) (equal? rest '(b c)))))))
+ (pass-if "list ..1"
+ (match '(a b c)
+ ((x ..1)
+ (equal? x '(a b c)))))
+
+ (pass-if "list ..1, with predicate"
+ (match '(a b c)
+ (((and x (? symbol?)) ..1)
+ (equal? x '(a b c)))))
+
(pass-if "tree"
(let ((tree '(one (two 2) (three 3 (and 4 (and 5))))))
(match tree
@@ -79,4 +89,15 @@
(pass-if-exception "tree"
exception:match-error
(match '(a (b c))
- ((foo (bar)) #t))))
+ ((foo (bar)) #t)))
+
+ (pass-if-exception "list ..1"
+ exception:match-error
+ (match '()
+ ((x ..1) #f)))
+
+ (pass-if-exception "list ..1, with predicate"
+ exception:match-error
+ (match '(a 0)
+ (((and x (? symbol?)) ..1)
+ (equal? x '(a b c))))))
next prev parent reply other threads:[~2010-09-19 21:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-05 15:04 ‘match’ and “k or more” patterns Ludovic Courtès
2010-09-06 1:46 ` Alex Shinn
2010-09-06 12:12 ` Ludovic Courtès
2010-09-08 2:18 ` Alex Shinn
2010-09-08 12:06 ` Ludovic Courtès
2010-09-08 18:53 ` Andy Wingo
2010-09-19 21:30 ` Ludovic Courtès [this message]
[not found] ` <2083922817.3379621.1285477344869.JavaMail.root@zmbs1.inria.fr>
2010-09-27 21:56 ` Ludovic Courtès
2010-09-28 1:20 ` Alex Shinn
[not found] ` <704726234.3471946.1285636813715.JavaMail.root@zmbs1.inria.fr>
2010-09-28 10:27 ` Ludovic Courtès
2010-10-02 7:13 ` Alex Shinn
2010-10-03 10:24 ` Andy Wingo
[not found] ` <2087670639.297243.1286101256715.JavaMail.root@zmbs1.inria.fr>
2010-10-03 12:35 ` Ludovic Courtès
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=87wrqhjua4.fsf@gnu.org \
--to=ludo@gnu.org \
--cc=alexshinn@gmail.com \
--cc=guile-devel@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).