all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: swedebugia@riseup.net
Cc: guix-devel@gnu.org
Subject: Re: Learning the match-syntax...
Date: Sat, 05 Jan 2019 23:35:23 +0100	[thread overview]
Message-ID: <87d0paspg4.fsf@elephly.net> (raw)
In-Reply-To: <c446d9551edfc72694c566d7ad90cae0@riseup.net>


Hi swedebugia,

> (define (metadata-ref file lookup)
>                       ^ 2 arguments
>   (fold (lambda (record acc)
>                   ^2 formals, why the acc?

“fold” 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 “acc”).

The accumulator first gets the initial value; in this case that’s #F.
The return value of the procedure is the new value of the accumulator.
The body is essentially just an “if” expression: the new value of the
accumulator is either whatever the “match” expression returns or it is
the unchanged value “acc”.

But nothing of this has anything to do with “match”.  Let’s 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 “record”, assuming 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 “greeting”, “object”, and
“question” (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 “val” is a two element list where the car is
'string-pat.  It returns the cadr of “val” (cadr = 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

  reply	other threads:[~2019-01-05 22:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18 20:45 `guix lint' warn of GitHub autogenerated source tarballs Arun Isaac
2018-12-19  8:43 ` Pierre Neidhardt
2018-12-19 14:07 ` Ludovic Courtès
2018-12-19 14:33   ` Efraim Flashner
2018-12-19 17:43     ` Arun Isaac
2018-12-19 19:29       ` Efraim Flashner
2018-12-21 20:50         ` Ludovic Courtès
2018-12-21 21:00           ` swedebugia
2018-12-25 14:32           ` Efraim Flashner
2019-01-05 17:39             ` Ludovic Courtès
2019-01-05 21:25               ` Learning the match-syntax swedebugia
2019-01-05 22:35                 ` Ricardo Wurmus [this message]
2019-01-06 21:36                 ` Chris Marusich
2019-01-07 17:34                   ` swedebugia
2019-01-07 22:18                     ` Ricardo Wurmus
2019-01-08  8:25                       ` swedebugia
2019-01-08 20:32                         ` Ricardo Wurmus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d0paspg4.fsf@elephly.net \
    --to=rekado@elephly.net \
    --cc=guix-devel@gnu.org \
    --cc=swedebugia@riseup.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.