Path: csiph.com!xmission!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: "Costello, Roger L." Newsgroups: comp.compilers Subject: Re: Best language for implementing compilers? Date: Tue, 12 Feb 2019 17:27:11 +0000 Organization: Compilers Central Lines: 45 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-02-010@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8bit 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 X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:2162 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]