【发布时间】:2014-03-17 02:55:38
【问题描述】:
我想做以下事情:
假设我们有一个数据列表 (3 2 1 5 4)
我想通过获取列表的前两个元素(汽车列表)和(cadr 列表)并将其与列表的其余部分(cddr 列表)连接来构造一个新列表。
在我的测试过程中,我尝试了多种方法来实现这一点,但都没有达到我想要的效果。在测试中,我正在寻找的结果是 (2 3 1 5 4)。
这是我尝试过的事情。
> (define b1 '( 3 2 1 5 4))
> b1
(3 2 1 5 4)
> (list (cadr b1) (car b1) (cddr b1))
(2 3 (1 5 4)) -- notice here that it becomes a list inside of a list.
> (cons (cadr b1) (cons (car b1) (cons (cddr b1) '())))
(2 3 (1 5 4)) -- notice here that it becomes a list inside of a list.
> (list (cons (cadr b1) (car b1)) (cddr b1))
((2 . 3) (1 5 4)) -- Something strange happens here, we get a period.
【问题讨论】:
标签: scheme