[racket users] list question

28 views
Skip to first unread message

Kevin Forchione

unread,
Feb 25, 2021, 12:07:31 PM2/25/21
to Racket-Users List
Hi guys,
I’m trying to find out what this process may be called. Suppose you have lists of various lengths:

‘(A B C D E F)
‘(1 2)
‘(3 4 5)

And you want tho produce the following:

'((A 1 3) (B 2 4) (C 1 5) (D 2 3) (E 1 4) (F 2 5) (A 1 3) (B 2 4) (C 1 5) (D 2 3) ...)

As you can see each element of the sublist corresponds to the list-ref of the modulo n (length sublist) as n proceeds from 0 to some maximum value. I’ve created a function that does exactly this, but … I’ve no idea what this process might be called. As they say, the naming of cats, and all that.Does this sort of mapping have a formal name?

Thanks!
Kevin

Norman Gray

unread,
Feb 25, 2021, 12:12:21 PM2/25/21
to Kevin Forchione, Racket-Users List

Kevin, hello.

On 25 Feb 2021, at 17:07, Kevin Forchione wrote:

> As you can see each element of the sublist corresponds to the list-ref
> of the modulo n (length sublist) as n proceeds from 0 to some maximum
> value. I’ve created a function that does exactly this, but …
> I’ve no idea what this process might be called. As they say, the
> naming of cats, and all that.Does this sort of mapping have a formal
> name?

I think this is called 'zip', or a convolution [1]. The variant you
describe is (effectively) with circular lists, but seems to be the same
principle.

...and I see that, with that name in hand, SRFI/1 does indeed have a zip
procedure, which works with circular lists.

[1] https://en.wikipedia.org/wiki/Convolution_(computer_science)

Best wishes,

Norman


--
Norman Gray : https://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK

Kevin Forchione

unread,
Feb 25, 2021, 2:13:51 PM2/25/21
to Norman Gray, Racket-Users List


> On Feb 25, 2021, at 10:12 AM, Norman Gray <norma...@glasgow.ac.uk> wrote:
>
>
> I think this is called 'zip', or a convolution [1]. The variant you describe is (effectively) with circular lists, but seems to be the same principle.
>
> ...and I see that, with that name in hand, SRFI/1 does indeed have a zip procedure, which works with circular lists.

Thanks, Norman! Looks like “zip” is the right name for the general process, and now that I can browse the docs for it there seems to be several flavors in various library modules, depending on creator intention. I’ve generalized mine a bit to fit intention and some of theirs.

(define/contract (zip #:length (len max) . lsts)
(->* () (#:length (or/c procedure? natural?)) #:rest (listof list?) (listof list?))
(define (loop cnt (acc empty))
(cond
[(zero? cnt) acc]
[else
(define n (sub1 cnt))
(loop n (cons (map (λ (lst) (list-ref lst (modulo n (length lst)))) lsts) acc))]))
(loop (cond
[(procedure? len)
(apply len (map length lsts))]
[else len])))

Kevin

Reply all
Reply to author
Forward
0 new messages