[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
pgp09cOfbBfQi.pgp
Description: OpenPGP digital signature
- scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/19
- Re: scheme-question about accumulating lists of lists, Malte Meyn, 2019/04/19
- Re: scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/19
- Re: scheme-question about accumulating lists of lists, David Pirotte, 2019/04/19
- Re: scheme-question about accumulating lists of lists,
David Pirotte <=
- Re: scheme-question about accumulating lists of lists, David Pirotte, 2019/04/19
- Re: scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/20
- Re: scheme-question about accumulating lists of lists, David Pirotte, 2019/04/20
- Re: scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/21
- Re: scheme-question about accumulating lists of lists, David Pirotte, 2019/04/21
- Re: scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/22
- Re: scheme-question about accumulating lists of lists, David Pirotte, 2019/04/22
- Re: scheme-question about accumulating lists of lists, Thomas Morley, 2019/04/23