如何在普通Lisp中从具有奇偶对的单个列表创建列表列表 [英] How to create list of lists from one single list with odd and even pair in common lisp

查看:58
本文介绍了如何在普通Lisp中从具有奇偶对的单个列表创建列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Lisp编程的新手,它试图从Lisp中的单个列表创建一个子列表,并从列表中创建一对奇数和偶数.例如: 我有一个清单

I am new in Lisp programming and trying to create sublists from one single list in lisp with pair of odd and even from the list. for example: I have a list

ListA ("a" "b" "c" "d" "e" "f" "g" "h") 

现在我要转换为以下列表:

now I want to convert into the following list:

enter code here
ListB  ( ("a" "b") ("c" "d") ("e" "f") ("g" "h") )

因此总是会以(((第一秒)(三分之四)(五分之六)............)

so always sublist will be generated with the value of ( (first second) (third fourth) (fifth sixth) ............)

我已经尝试了多种方法,例如首先将奇数项和偶数项分开并使用了该函数(列表(oddlist偶数列表)),但没有达到上述ListB中的期望值.有人可以在这方面帮助我.非常感谢您的帮助.

I have tried with mutiple ways for example first take out odd item and even item separate and used the function (list (oddlist evenlist)) but not getting above expected values in the above ListB.Could someone please help me in this regard. Your help would be highly appreciated.

推荐答案

这实际上是非常且带有 loop 的缩写:

This is actually very short with loop:

(loop for (x y) on '(a b c d e f) by #'cddr
      collect (list x y))
;=> ((A B) (C D) (E F))

如果元素的数量为奇数,这的确会在最后一对中为您提供NIL,但是您没有提及在这种情况下应该发生的情况:

This does give you a NIL in the last pair if you have an odd number of elements, but you didn't mention what should happen in that case:

(loop for (x y) on '(a b c d e f g ) by #'cddr
      collect (list x y))
;=> ((A B) (C D) (E F) (G NIL))

这篇关于如何在普通Lisp中从具有奇偶对的单个列表创建列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆