From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Richard Stallman Newsgroups: gmane.emacs.devel Subject: Re: Instead of pcase Date: Fri, 22 Dec 2023 21:53:34 -0500 Message-ID: References: Reply-To: rms@gnu.org Content-Type: text/plain; charset=Utf-8 Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38283"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Adam Porter Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Dec 23 03:54:27 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rGs9i-0009pc-Nu for ged-emacs-devel@m.gmane-mx.org; Sat, 23 Dec 2023 03:54:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rGs8w-0000vB-8x; Fri, 22 Dec 2023 21:53:38 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rGs8t-0000uX-3T for emacs-devel@gnu.org; Fri, 22 Dec 2023 21:53:36 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rGs8s-0007ND-RW; Fri, 22 Dec 2023 21:53:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=Date:References:Subject:In-Reply-To:To:From: mime-version; bh=60DJFQpLNABDB4nl4BDGUQybOIplFHLTAA/+dreXMtQ=; b=dyy5ZbRqKCk2 7MWAwdXkOGcPuAGGJTOuAra9swCr4GA81zO88KoTDGWVnoZOGEx0j+XJA54V2tnJXbnBkDnShC+ic vLvjrV0Z/I+p/TBG8wuUvi0efIy9iMT+JSBz+AhGbBpoIDBNwHFE86R96ottlzHBI9bBxUj7y0yUr zPwK7EP9fU49fa03TAuUgDo3vbqB9tWNbGePKzioTaJeVIKsgrUwRBXux9ysfN9z+ybWMEnvd/hTM t0X4xGiAYbpCixx1gtriRWEJixq6V/ZRaANrQZtFBP2UAmAPKRtH08wyUsXTu5XLI2IGSomkZWcKA NXpNG9r4VEFH1eZ3Zdxhdw==; Original-Received: from rms by fencepost.gnu.org with local (Exim 4.90_1) (envelope-from ) id 1rGs8s-0005TI-JX; Fri, 22 Dec 2023 21:53:34 -0500 In-Reply-To: (message from Adam Porter on Wed, 20 Dec 2023 04:52:17 -0600) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:314090 Archived-At: [[[ To any NSA and FBI agents reading my email: please consider ]]] [[[ whether defending the US Constitution against all enemies, ]]] [[[ foreign or domestic, requires you to follow Snowden's example. ]]] > Or what if TESTFN needs to receive multiple arguments bound from a > matched pattern? What if TESTFN needs 3 arguments, and the middle one > is not from the matched pattern? Or what if the middle one /is/ from > the matched pattern? Thank you. I did not realize that my description was unclear in this way. I've done a lot of rewriting; I hope this version is clearer. Constrained variable: (PRED VARIABLE) or, more generally, (PRED VARIABLE OTHER-ARGS...) This matches any value VALUE that satisfies a specified constraint. PRED is a function to test the constraint. It receives VALUE as an argument. If PRED returns true, that means VALUE matches the constraint, so this pattern binds (or sets) VARIABLE to that value. For instance, (symbolp sym) ; Match any symbol, bind `sym' to it. If you wish, you can specify additional arguments to pass to PRED. The OTHER-ARGS are not patterns to match, they are Lisp expressions to whose values specify the additional arguments to pass to PRED, following the first argument which is VALUE. In effect, the call to PRED looks like this: (apply PRED VALUE (mapcar 'eval OTHER-ARGS)) For example, (> num-foos 1) ; Match any number greater than 1, bind `num-foos' to it. (> num-foos min-foos) ; Match any number greater than MIN-FOOS, ; bind `num-foos' to it. When matching to bind variables, the presence of this kind of subpattern in the overall pattern to be matched unconditionally binds VARIABLE whether the subpattern matches or not. Constrained variable constructs can be nested For example, (< (integerp num-foos) 1) ; Match any integer number > 1, ; bind `num-foos' to it. Advanced hack for experts: the OTHER-ARGS can refer to variables bound earlier in this pattern-matching operation. For example, `(,(integerp smaller) ,(> (integerp bigger) smaller)) ; Matches a list of two integers in which the second integer ; is larger than the first. (or `(,(integerp smaller) ,(> (integerp bigger) smaller)) `(,(integerp bigger) ,(< (integerp smaller) bigger))) ; matches a ist of two integers and binds `bigger' to the bigger one ; and `smaller' to the smaller one. However, it might be cleaner to match the two integers regardless of their numberical ordering, then in Lisp code see which one is larger. General constrained variable: (constrain VAR EXPRESSION) This general constrained variable pattern binds VAR to the value being matched against, tentatively, then evaluates EXPRESSION. If the result is true, the match succeeds and leaves VAR bound to that value. For instance, (constrain x (and (> x 0) (< x 100))) succeeds if the value being matched aainst is in the open interval (0, 100), and in that case it binds x to that value. -- Dr Richard Stallman (https://stallman.org) Chief GNUisance of the GNU Project (https://gnu.org) Founder, Free Software Foundation (https://fsf.org) Internet Hall-of-Famer (https://internethalloffame.org)