From mboxrd@z Thu Jan 1 00:00:00 1970 From: swedebugia@riseup.net Subject: Learning the match-syntax... Date: Sat, 05 Jan 2019 13:25:40 -0800 Message-ID: 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> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggsout.gnu.org ([209.51.188.92]:54156 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gftSB-00071q-HH for guix-devel@gnu.org; Sat, 05 Jan 2019 16:26:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gftSA-0002oW-Nt for guix-devel@gnu.org; Sat, 05 Jan 2019 16:25:59 -0500 In-Reply-To: <87zhsf2ec6.fsf@gnu.org> 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: =?UTF-8?Q?Ludovic_Court=C3=A8s?= Cc: guix-devel@gnu.org, Guix-devel Saluton Ludo' :) I write this to you for no other reason than I like the code your write and would love to learn more. If you don't have time/energy just let me know and I will ask Pierre or somebody else. :) On 2019-01-05 18:39, Ludovic Court=C3=A8s wrote: > One last thing: >=20 >> +(define (check-source-unstable-tarball package) >> + "Emit a warning if PACKAGE's source is an autogenerated tarball." >> + (define (check-source-uri uri) >> + (when (and (string=3D? (uri-host (string->uri uri)) "github.com") >> + (string=3D? (third (split-and-decode-uri-path >> + (uri-path (string->uri uri)))) >> + "archive")) >=20 > =E2=80=98third=E2=80=99 could fail badly if the list has fewer elements= , so I=E2=80=99d suggest > writing it something like: >=20 > (when (and =E2=80=A6 > (match (split-and-decode-uri-path =E2=80=A6) > ((_ _ "archive" _ ...) #t) > (_ #f))) > =E2=80=A6) This is an elegant rewrite to return a boolean #f in case the URL is bad in some way. I'm trying very hard to learn the guile match-syntax. To make sure I understand the above I would like to explain it and you can verify that I got it right. Ok? > (match (split-and-decode-uri-path =E2=80=A6) <- this is th= e input > ((_ _ "archive" _ ...) #t) ^ ^ ^ =3Dthird ^ ^return true if the clause match ^fourth & more ^ ^ =3D anything twice=20 > (_ #f))) ^ =3Dthe general case =3Delse return #f. Correct? This is a more complicated (nested) match example from Juliens opam importer which I find is an elegant functional way to find what we need: (define (metadata-ref file lookup) (fold (lambda (record acc) (match record ((record key val) (if (equal? key lookup) (match val (('list-pat . stuff) stuff) (('string-pat stuff) stuff) (('multiline-string stuff) stuff) (('dict records ...) records)) acc)))) #f file)) (define (metadata-ref file lookup) ^ 2 arguments (fold (lambda (record acc) ^2 formals, why the acc? (match record ^input ((record key val) ^match "record" "key" "val" in a list? (if (equal? key lookup) ^ we continue if the key is right (match val ^input (('list-pat . stuff) stuff) ^ if 'list-pat return stuff (('string-pat stuff) stuff) ^ if 'string-pat return stuff (('multiline-string stuff) stuff) (('dict records ...) records)) ^if 'dict return first record acc)))) ^ this is the else (what is this good for?) #f file)) ^ input to fold ^ no initial in the fold If I understood this correctly I hope I will be able to get something like this to work in the quicklisp importer :) --=20 Cheers=20 Swedebugia