From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ricardo Wurmus Subject: Re: Learning the match-syntax... Date: Mon, 07 Jan 2019 23:18:28 +0100 Message-ID: <87bm4s862z.fsf@elephly.net> References: <87pntxwqx0.fsf@gnu.org> <08635A1A-EDA5-44B0-8C8A-532F16683154@flashner.co.il> <20181219192926.GB2581@macbook41> <87imzmmwno.fsf@gnu.org> <20181225143202.GO2581@macbook41> <87zhsf2ec6.fsf@gnu.org> <878szx1nah.fsf@gmail.com> <9ee2cc12-7394-6256-7268-adc43c1a56c9@riseup.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([209.51.188.92]:41045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ggdT7-00080w-3r for guix-devel@gnu.org; Mon, 07 Jan 2019 17:34:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ggdT6-000352-1e for guix-devel@gnu.org; Mon, 07 Jan 2019 17:34:01 -0500 Received: from sender-of-o53.zoho.com ([135.84.80.218]:21796) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ggdT1-0002uR-Py for guix-devel@gnu.org; Mon, 07 Jan 2019 17:33:56 -0500 In-reply-to: <9ee2cc12-7394-6256-7268-adc43c1a56c9@riseup.net> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: swedebugia Cc: "guix-devel@gnu.org" Hej swedebugia, > e.g. > (match '(1 2 "y" "x") > (1 > 'one) > (number > 'number)) > > Will match any number for the first clause and any string for the > second. It ONLY checks if it is a number. This is not correct. The first clause does not match because the number 1 is not equal to the list '(1 2 "y" "x"). So we fall through to the next pattern. That pattern is just the variable name =E2=80=9Cnumber=E2=80= =9D, which will match absolutely anything. =E2=80=9Cnumber=E2=80=9D will be bound to = '(1 2 "y" "x") and the return value of the clause is the symbol 'number. > To actually match something e.g. the "x" literally we need to nest the > match like this: > > (match '(1 2 y x) > (string > (match string > ("x" > 'x!))) > (number > 'number)) No. Here the first pattern matches absolutely anything. The name =E2=80=9Cstring=E2=80=9D could be anything at all. It=E2=80=99s just the n= ame of a variable that should be bound. So here you first bind '(1 2 y x) to the variable =E2=80=9Cstring=E2=80=9D, and then you try match the value of that very sam= e variable to the string "x", which fails. Hence we fall through to the next clause, where the pattern is just the variable =E2=80=9Cnumber=E2=80=9D, which will= bind to absolutely anything. So that=E2=80=99s what happens and the symbol 'number= is returned. > Positional arguments work like this: > > (match '(1 2 y x) > ;match the third item > (_ _ string > ;check if it is the literal "x" > (match string > ("x" > 'x!))) > (number > 'number)) > > Correct? No. If you run this in the REPL you=E2=80=99ll see an error. You have misunderstood how match works. Here=E2=80=99s another example: (match '(1 2 x y) ((one two three four) (format #f "the first value is ~a, the second is ~a, the third is ~a and th= e fourth is ~a\n" one two three four)) ((this will never match) #f)) Here we have two clauses: the first clause has the pattern (one two three four) i.e. a list of four variables. This matches the value exactly. Each variable is bound to one of the values of the list. The second clause has also four variables and would match just as well, but it will not be considered as the first pattern has already matched. Does this make things clearer? To match by *type* (as you tried above) you need to use predicates. Here=E2=80=99s an example: (match '(1 2 x y) (((? string? one) two three four) 'will-not-match) ((this (? number? will) totally match) will)) The first pattern would only match if the first value were a string (which would be bound to the variable =E2=80=9Cone=E2=80=9D). But it is no= t, so the next pattern is tried. There we want to match against four variables of which the second needs to be a number. This matches, so =E2=80=9Cwill=E2= =80=9D is bound to the number 2, which is what we return. -- Ricardo