From: | "Costello, Roger L." <costello@mitre.org> |
Newsgroups: | comp.compilers |
Date: | Tue, 12 Feb 2019 17:27:11 +0000 |
Organization: | Compilers Central |
Injection-Info: | gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="53857"; mail-complaints-to="abuse@iecc.com" |
Keywords: | ML, comment |
Posted-Date: | 12 Feb 2019 12:53:07 EST |
Kaz Kylheku wrote:
-------------------------------------------------
Suppose we want to check whether we have an expression that is the binary
product of two binary sums, as in (* (+ a b) (+ c d)).
Without pattern matching:
(when (and (eq (car expr) '*) ;; starts with *
(consp (cdr expr)) ;; has an argument
(consp (cddr expr)) ;; has another argument
(null (cdddr expr)) ;; then the list ends
(consp (cadr expr)) ;; first arg is a compound
(eq (cadr expr) '+) ;; ... starting with a +
... ;; etc
(do-whatever ...))
With very rudimentary pattern matching (simple destructuring):
(destructuring-when (op1 (op2 a b) (op3 c d)) expr
(when (equal (list op1 op2 op3) '(* + +))
(do-whatever ...)))
With pattern matching:
(when-match expr (* (+ ?a ?b) (+ ?c ?d))
(do-whatever ...) ;; ?a ?b ... are in scope bound to subtrees
...)
-------------------------------------------------
Wow!
That is a fantastic example.
Are there programming languages that have the pattern matching capability
shown in Kaz's last example?
What programming language has the best pattern matching capability?
Is the programming language with the best pattern matching capability the best
language for implementing compilers?
/Roger
[Why do you think we're talking about ML? It has matching as a
primitive. I don't recall any lisp-ish languages with built in tree
matchers but it's easy enough to do that it's often an exercise in the
introductory programming class. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.