Kane
Kane

Reputation: 898

how to get two elements from a list each time in common-lisp?

For ((1 2) (3 4) (5 6)), you can use loop for (a b) in '((1 2) (3 4) (5 6)) to get a = 1, b = 2 each time.

But for '(1 2 3 4), I try to do loop for x on '(1 2 3 4) by #'cddr for a = (car x) for b = (cadr x) to get a = 1, b = 2.

Is there a better way to do such thing?

Upvotes: 4

Views: 1174

Answers (1)

huaiyuan
huaiyuan

Reputation: 26549

Try for ... on:

(loop for (a b) on '(1 2 3 4) by #'cddr collect (cons a b))

Upvotes: 10

Related Questions