[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:52:39 -0300 |
Hi again,
Replying twice to myself in a row, how is that :)
A little tired I guess ...
> > 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:
And here is a corrected version: the code in the mail I'm answering now would
not
return proper (expected) results for any other operator then car ... this one
will
let you really walk :)
(use-modules (ice-9 match))
(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) '()))))
(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 (proc (cons a rest))
result))))
(()
(reverse! result)))))
scheme@(guile-user)> (load "blue.scm")
;;; note: source file /usr/alto/projects/guile/blue.scm
;;; ...
scheme@(guile-user)> (blue-walk blue car)
$2 = (a 1 x)
scheme@(guile-user)> (blue-walk fox car)
$3 = (a 1 x)
scheme@(guile-user)> (blue-walk blue cdr)
$4 = ((b c) (2 3) (y z))
scheme@(guile-user)> (blue-walk fox cdr)
$5 = ((b c) (2 3) (y z))
Cheers,
David
pgpeXtmrRbrGy.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, 2019/04/19
- Re: scheme-question about accumulating lists of lists,
David Pirotte <=
- 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