【问题标题】:Appending list in lisp在 lisp 中附加列表
【发布时间】:2017-03-19 14:04:05
【问题描述】:

在 lisp 中,我将列表附加为: (setq newlist (append (side a b)(this a b) (that a b) ))

这会将所有必需的列表附加为: (1 0 0 0 2 0 4 0 6 0) 但我想要的是这样的: ((1 0)(0 0)(2 0)(4 0)(6 0))

我应该怎么做才能获得所需的格式。请在 lisp 中发布代码示例。

【问题讨论】:

  • 什么是sidethisthat
  • 它似乎不是您要查找的附加信息。也许(list '(side a b) '(this a b) '(that a b)))
  • @sds 方面,this 和that 是其他三个有列表的函数。像边包含:(1 0 0 0),这包含:(0 0 2 0),并且包含:(4 0 6 0)。当我附加它们时,我得到 (1 0 0 0 0 0 2 0 4 0 6 0) 但我需要的是 ((1 0) (0 0) (0 0 ) (2 0) (4 0) (6 0 ))
  • @Sylwester (list '(side ab) '(this ab) '(that ab)) 给出 list 和 (list (side ab) (this ab) (that ab) 中相同元素的集合) 给出这样的结果: ((1 0) (0 0 0 3) (0 0 4 0)) 但我需要的是 ((1 0) (0 0) ( 0 3) (0 0 ) (4 0) )。
  • 函数不“包含”列表。他们可能退回他们。请编辑您的问题以包含我们在 cmets 中要求的其他信息。

标签: list recursion append lisp


【解决方案1】:

所以实际上你只需要在添加元素后重新构造它:

(loop :for (e1 e2) 
      :on '(1 0 0 0 2 0 4 0 6 0) 
      :by #'cddr 
      :collect (list e1 e2))

; ==> ((1 0) (0 0) (2 0) (4 0) (6 0))

建议阅读是LOOP for black belts,我在这里使用的你应该注意的部分是“循环集合和包”和“解构变量”。这大概是Practical Common Lisp我读得最多的一章了。整本书都很好,所以每个 lisper 都应该知道。

【讨论】:

  • 感谢您的回复。 :)
猜你喜欢
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多