From: Blake Shaw <blake@reproduciblemedia.com>
To: guile-devel@gnu.org
Cc: Blake Shaw <blake@reproduciblemedia.com>
Subject: [PATCH v1 6/6] docs/match:style reviewing with pdf, adding newlines
Date: Fri, 27 Jan 2023 01:58:01 +0700 [thread overview]
Message-ID: <20230126185801.19064-6-blake@reproduciblemedia.com> (raw)
In-Reply-To: <20230126185801.19064-1-blake@reproduciblemedia.com>
---
doc/ref/match.texi | 78 ++++++++++++++++++++++++++++------------------
1 file changed, 47 insertions(+), 31 deletions(-)
diff --git a/doc/ref/match.texi b/doc/ref/match.texi
index 5512103c4..38745e173 100644
--- a/doc/ref/match.texi
+++ b/doc/ref/match.texi
@@ -19,15 +19,15 @@
@cindex pattern matching
@cindex (ice-9 match)
-The @code{(ice-9 match)} module provides a @dfn{pattern matcher},
+@* The @code{(ice-9 match)} module provides a @dfn{pattern matcher},
written by Alex Shinn, and compatible with Andrew K. Wright's pattern
matcher found in many Scheme implementations.
-A pattern matcher does precisely what the name implies: it matches
-some arbitrary pattern, and returns some result accordingly.
-
-@lisp
+@* @noindent A pattern matcher does precisely what the name implies: it
+matches some arbitrary pattern, and returns some result accordingly.
+@*
+@example
(define (english-base-ten->number name)
(match name
('zero 0)
@@ -40,40 +40,43 @@ some arbitrary pattern, and returns some result accordingly.
('seven 7)
('eight 8)
('nine 9)))
-
-@end lisp
+@end example
-@lisp
+@*
+@example
(english-base-ten->number 'six)
-
@result{} 6
-@end lisp
+@end example
-@lisp
+@example
(apply + (map english-base-ten->number '(one two three four)))
@result{} 10
-@end lisp
+@end example
-@cindex pattern variable
-Pattern matchers may contain @dfn{pattern variables}, local bindings
-to all elements that match a pattern.
+@*
+@page
+@cindex pattern variable
+@noindent Pattern matchers may contain @dfn{pattern variables},
+local bindings to all elements that match a pattern.
-@example
-(let re ((ns '(one two three four ten))
- (total 0))
+@example
+
+(let re ([ns '(one two three four 9)] [total 0])
(match ns
- ((e) (+ total (english-base-ten->number e)))
- ((e . es)
- (re es (+ total (english-base-ten->number e))))))
-@result{} 20
+ [(e) (+ total (english-base-ten->number e))]
+ [(e . es)
+ (re es (+ total (english-base-ten->number e)))]))
+
+@result{} 19
@end example
-In this example, the list @var{ns} matches the pattern
+@*
+@noindent In this example, the list @var{ns} matches the pattern
@code{(e . es)}, where the pattern variable @var{e} corresponds
-to the `car` of @var{ns} and the pattern variable @var{es}
-corresponds to the `cdr` of @var{ns}.
+to the metaphoical "car" of @var{ns} and the pattern variable @var{es}
+corresponds to the "cdr" of @var{ns}.
-A tail call @code{re} is then initiated and we `cdr` down the
+A tail call @code{re} is then initiated and we "cdr" down the
list by recurring on the tail @var{es}, applying our matcher
@code{english-base-ten->number} to each element of @var{ns} until
only a single element @code{(e)} remains, causing the @var{total}
@@ -82,15 +85,16 @@ to be computed. In modern Scheme programming it is common to use
of @code{cond}, @code{car} and @code{cdr}, so it's important to
understand how these idioms translate.
+@*
Patterns can be composed and nested. For instance, @code{...}
(ellipsis) means that the previous pattern may be matched zero or more
times in a list:
@example
+
(match lst
- (((heads tails ...) ...)
- heads))
-@result{}
+ (((heads tails ...) ...) heads))
+
@end example
@noindent
@@ -104,25 +108,31 @@ A pattern matcher can match an object against several patterns and
extract the elements that make it up.
@example
+
(let ((m '((l1 . r1) (l2 . r2) (l3 . r3))))
(match m
(((left . right) ...)
(list left right))))
+
@result{} ((l1 l2 l3) (r1 r2 r3))
@end example
@example
+
(let ((m '((1 . (a . b)) (2 . (c . d)) (3 . (e . f)))))
(match m
(((key . (left . right)) ...)
(fold-right acons '() key right ))))
+
@result{} ((1 . b) (2 . d) (3 . f))
@end example
@example
+
(match '(((a b c) e f g) 1 2 3)
((((head ...) . rest) tails ...)
(acons tails head rest )))
+
@result {} (((1 2 3) a b c) e f g)
@end example
@@ -130,6 +140,7 @@ Patterns can represent any Scheme object: lists, strings, symbols,
records, etc.
@example
+
(let re ((m #(a "b" c "d" e "f" g)))
(match m
((or (e) #(e)) e)
@@ -137,7 +148,9 @@ records, etc.
(e1 e2 es ...))
(cons (cons e1 e2)
(re es)))))
+
@result{} ((a . "b") (c . "d") (e . "f") . g)
+
@end example
When a matching pattern is found, an expression associated with the
@@ -145,12 +158,15 @@ pattern is evaluated, optionally with all pattern variables bound to
the corresponding elements of the object.
@example
+
(let re ((m '(a b c d e f g h i)))
(match m
((e) e)
((e1 e2 es ...)
(acons e1 e2 (re es)))))
+
@result{} ((a . b) (c . d) (e . f) (g . h) . i)
+
@end example
Compared to hand-written code, pattern matching noticeably improves
@@ -161,7 +177,7 @@ pattern---conversely, hand-written code often trades robustness for
conciseness. And of course, @code{match} is a macro, and the code it
expands to is just as efficient as equivalent hand-written code.
-The pattern matcher is defined as follows:
+@noindent @* We define @emph{match} as follows. @*
@deffn {Scheme Syntax} match exp clause1 clause2 @dots{}
Match object @var{exp} against the patterns in @var{clause1}
@@ -188,7 +204,7 @@ arbitrary Scheme expression, possibly referring to pattern variables of
@c
@c clause ::= (pat body) | (pat => exp)
-The syntax and interpretation of patterns is as follows:
+@noindent @* The syntax and interpretation of patterns is as follows: @*
@verbatim
patterns: matches:
--
2.39.1
next prev parent reply other threads:[~2023-01-26 18:58 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-26 18:57 [PATCH v1 1/6] docs/match: add pattern matching examples Blake Shaw
2023-01-26 18:57 ` [PATCH v1 2/6] docs/match: rm unquote-splicing as it interferes with textinfo Blake Shaw
2023-01-27 16:27 ` Maxime Devos
2023-01-28 9:14 ` Blake Shaw
2023-01-28 13:08 ` Maxime Devos
2023-01-29 3:09 ` Blake Shaw
2023-01-26 18:57 ` [PATCH v1 3/6] docs/match: add reverse nested list example Blake Shaw
2023-01-26 18:57 ` [PATCH v1 4/6] docs/match: match-let* unwrap example Blake Shaw
2023-01-26 18:58 ` [PATCH v1 5/6] docs/fixup: @cindex was in the wrong place Blake Shaw
2023-01-26 18:58 ` Blake Shaw [this message]
2023-01-28 13:18 ` [PATCH v1 1/6] docs/match: add pattern matching examples Maxime Devos
2023-01-28 19:10 ` randomlooser
2023-01-28 19:23 ` randomlooser
[not found] ` <CAKjmbcA9TeuC0HWE53cG4EfautTcW5s9tyh0tCXUvicAQiBFKQ@mail.gmail.com>
2023-01-29 14:23 ` Maxime Devos
2023-01-28 13:48 ` Maxime Devos
[not found] ` <CAKjmbcAucYA9j7suY1gEAO512pn+90ED33Wq5Z7CjrBsqxgrbw@mail.gmail.com>
2023-01-29 15:30 ` Maxime Devos
2023-01-30 0:49 ` Blake Shaw
2023-01-30 19:56 ` Aleix Conchillo Flaqué
2023-01-30 19:57 ` Aleix Conchillo Flaqué
2023-01-31 2:53 ` Maxime Devos
2023-01-31 3:59 ` Aleix Conchillo Flaqué
2023-01-31 2:28 ` [PATCH v1 1/6] docs/match: add pattern matching examples + CoC Maxime Devos
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=20230126185801.19064-6-blake@reproduciblemedia.com \
--to=blake@reproduciblemedia.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).