lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scheme-question about accumulating lists of lists


From: David Pirotte
Subject: Re: scheme-question about accumulating lists of lists
Date: Sun, 21 Apr 2019 18:51:06 -0300

Hi Thomas,

> ...
> Thanks again!

You're welcome.

I used 'funny' (weird) procedure and variable names, but if the procedure is to 
be
exposed to your users, and with the objective of making it simple to use, read 
and
maintain, as you described later in your answer, you could write it as - using
(ice-9 match):

(define (lp-map proc lp-list)
  "Apply PROC to each element of LP-LIST. An LP-LIST must be a list of
lists of items - (cons '(a b c) (cons '(d e f) (cons '(g h i) '()))) -
or a list of lists of items plus a pair of list of items - (cons '(a b
c) (cons '(d e f) '(g h i))).  Each sublist item must
statisfy (not (pair? item)).  The result(s) of the procedure
applications are saved and returned in a list."
  (let loop ((lp-list lp-list)
             (result '()))
    (match lp-list
      ((elt . rest)
       (if (pair? elt)
           (loop rest
                 (cons (proc elt) result))
           (reverse! (cons (proc lp-list) result))))
      (()
       (reverse! result)))))

This version also matches the built-in map argument order. If you need an 
lp-map that
accepts more then an lp-list, let me know:   "lp-map proc lp-list1 lp-list2 ..."

Now, it is worth pointing that if the above code is ok, it is not very robust: 
it
strongly depends on well formed LP-LIST, and will fail otherwise, as (not
very well) described in the procedure comment, consider this example:

        (define bad-lp-list
          (cons '(a b c) (cons '(1 2 3) '((x) y z))))

        scheme@(guile-user)> (lp-map car bad-lp-list)
        $2 = (a 1 x y)

> I think you're wrong.
> It's not in the guile-1.8-docs, but below worked:

Oh, great.

> ...
> Among keeping things simple is the attempt to use very little
> additional guile-modules.

I am all in favor of simplicity (who would not?), though I don't agree that not
using additional Guile modules does (always) help to achieve that goal.

> Whether 'pattern matching' will be useful to hide complexity to make
> life easier for our users or whether it adds an abstraction layer,
> which would make it even harder for users to write their own
> guile-code, I can't judge currently.

Ok, I believe it does help a lot, maybe even more scheme beginners actually, 
and so
do our maintainers, here is an extract of the (latest) manual, section "6.6.8 
Pairs":

        ...
        Since a very common operation in Scheme programs is to access the car 
of a
        car of a pair, or the car of the cdr of a pair, etc., the procedures 
called
        caar, cadr and so on are also predefined. However, using these 
procedures is
        often detrimental to readability, and error-prone. Thus, accessing the
        contents of a list is usually better achieved using pattern matching
        techniques (see Pattern Matching).
        ...

But of course do as you wish, there is nothing 'wrong' writing good scheme code 
using
'old' destructuring techniques, but it will, always imo, be more difficult to 
read
and maintain.

David.

Attachment: pgpkrQ9F3K9EO.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]