From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ricardo Wurmus Subject: Re: Learning the match-syntax... Date: Sat, 05 Jan 2019 23:35:23 +0100 Message-ID: <87d0paspg4.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> 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]:49555 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gfuXs-0003Do-ML for guix-devel@gnu.org; Sat, 05 Jan 2019 17:35:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gfuXp-0004Je-Kz for guix-devel@gnu.org; Sat, 05 Jan 2019 17:35:56 -0500 Received: from sender-of-o53.zoho.com ([135.84.80.218]:21709) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gfuXp-0004IG-Cn for guix-devel@gnu.org; Sat, 05 Jan 2019 17:35:53 -0500 In-reply-to: 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@riseup.net Cc: guix-devel@gnu.org Hi swedebugia, > (define (metadata-ref file lookup) > ^ 2 arguments > (fold (lambda (record acc) > ^2 formals, why the acc? =E2=80=9Cfold=E2=80=9D is a higher order function that takes a two-argument= procedure (the lambda here), an initial value, and a list to fold over. The procedure that is provided as the first argument is applied to each element of the list *and* is given the current value of the so-called accumulator (here bound to =E2=80=9Cacc=E2=80=9D). The accumulator first gets the initial value; in this case that=E2=80=99s #= F. The return value of the procedure is the new value of the accumulator. The body is essentially just an =E2=80=9Cif=E2=80=9D expression: the new va= lue of the accumulator is either whatever the =E2=80=9Cmatch=E2=80=9D expression retur= ns or it is the unchanged value =E2=80=9Cacc=E2=80=9D. But nothing of this has anything to do with =E2=80=9Cmatch=E2=80=9D. Let= =E2=80=99s look at the match expressions. > (match record > ^input > ((record key val) > ^match "record" "key" "val" in a list? This is really destructuring the value of =E2=80=9Crecord=E2=80=9D, assumin= g that it is a three element list, and binding each of the elements to a variable. Try this: --8<---------------cut here---------------start------------->8--- (use-modules (ice-9 match)) (match '(hello world how are you ?) ((greeting object . question) (length question))) --8<---------------cut here---------------end--------------->8--- This will match the quoted expression against a pattern that binds a list with at least two values to the variables =E2=80=9Cgreeting=E2=80=9D, = =E2=80=9Cobject=E2=80=9D, and =E2=80=9Cquestion=E2=80=9D (for everything after the first two values). > (match val > ^input > (('list-pat . stuff) stuff) > ^ if 'list-pat return stuff If (eq? (car val) 'list-pat) then return (cdr val), even if that might be the empty list. > (('string-pat stuff) stuff) > ^ if 'string-pat return stuff This matches if =E2=80=9Cval=E2=80=9D is a two element list where the car is 'string-pat. It returns the cadr of =E2=80=9Cval=E2=80=9D (cadr =3D car of= the cdr). > (('multiline-string stuff) stuff) Similar here. > (('dict records ...) records)) > ^if 'dict return first record Close. It returns all the values following 'dict. This is the same as if the pattern ('dict . records) had been used. Hope this helps! -- Ricardo