[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 21:23:05 -0300 |
Hi Thomas,
> Failing example:
> (map
> car
> (cons '(a b c) (cons '(1 2 3) '(x y z))))
> One way to make it work is to convert the initial pair (cons '(1 2 3)
> '(x y z)) to a list of lists, i.e (cons '(1 2 3) (list '(x y z)))
> The question is: is it the only and/or best way?
It sounds a lot cleaner to me, because, in the end, it is as if:
(cons '(a b c) (cons '(1 2 3) (cons '(x y z) '())))
which is 'consistent all the way down, so to speak, and will let you use
build-in
scheme ops like map, fold, ...
If you want to keep the original structure though, I'd use (ice-9 match) and
recurse:
(use-modules (ice-9 match))
(define blue
(cons '(a b c) (cons '(1 2 3) '(x y z))))
(define (blue-walk blue proc)
(let loop
((blue blue)
(result '()))
(match blue
((x y z)
(reverse! (cons x
result)))
((a . rest)
(loop rest
(cons (proc a) result))))))
scheme@(guile-user)> (load "blue.scm")
;;; compiling /usr/alto/projects/guile/blue.scm
;;; compiled
/home/david/.cache/guile/ccache/2.2-LE-8-3.A/usr/alto/projects/guile/blue.scm.go
scheme@(guile-user)> (blue-walk blue car)
$8 = (a 1 x)
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.
David
pgprLAfDb5EJ6.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 <=
- 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, 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