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: Fri, 19 Apr 2019 22:31:00 -0300

Hi again,

> Note that the above will only work if the last 'blue item' has 3 elements, 
> you'd
> need to adapt for other use case (which also 'speak' in favor of the cleaner
> approach.

Actually, I didn't like what I wrote, here is a slightly better code:

(use-modules (ice-9 match))

(define (blue-walk blue proc)
  (let loop ((blue blue)
             (result '()))
    (match blue
      ((a . rest)
       (if (pair? a)
           (loop rest
                 (cons (proc a) result))
           (reverse! (cons a result))))
      (()
       (reverse! result)))))

It solves the above note (it avoids to have to know how many elements the last 
pair
has), and also let you process both 'your' structure and  the 'cleaner' one:

(define blue
  (cons '(a b c) (cons '(1 2 3) '(x y z))))

(define fox
  (cons '(a b c) (cons '(1 2 3) (cons '(x y z) '()))))

scheme@(guile-user)> (load "blue.scm")
;;; note: source file /usr/alto/projects/guile/blue.scm
;;;       newer than ...
scheme@(guile-user)> (blue-walk fox car)
$2 = (a 1 x)
scheme@(guile-user)> (blue-walk blue car)
$3 = (a 1 x)


Cheers,
David

Attachment: pgp09cOfbBfQi.pgp
Description: OpenPGP digital signature


reply via email to

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