Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

List to tuples

5 views
Skip to first unread message

WJ

unread,
Jul 27, 2016, 9:40:00 AM7/27/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Frode Vatvedt Fjeld wrote:

> > say there's a list of even length
> > (a b c d e f ......)
> >
> > how do i turn this into
> > ((a b) (c d) (e f)....)
>
> The mapping idiom doesn't work so well here, since that's all about
> transforming each element individually, whereas here you process
> elements two by two (or the list as such in steps of two).
>
> I think this is the most natural solution in Common Lisp:
>
> (loop for (x y) on list by #'cddr collect (list x y))

OCaml:

let list = ["a";"b";"c";"d"];;

let rec g=function x::y::z->(x,y)::g z | _->[]
in g list;;

[("a", "b"); ("c", "d")]

--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K0hD7IffTJs)
0 new messages