什么是“利弊”将项目添加到列表的末尾? [英] what is the 'cons' to add an item to the end of the list?

查看:120
本文介绍了什么是“利弊”将项目添加到列表的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是将项目添加到列表的末尾典型的方法是什么?

what's the typical way to add an item to the end of the list?

我有一个列表(1 2 3),要添加4到它(其中图4是评价的结果(+ 2))

I have a list (1 2 3) and want to add 4 to it (where 4 is the result of an evaluation (+ 2 2))

(setf nlist '(1 2 3))  
(append nlist (+ 2 2))  

这表示,预计追加的列表,而不是数字。我将如何做到这一点?

This says that append expects a list, not a number. How would I accomplish this?

推荐答案

您可以使用追加,但要注意,如果在一个循环或很长的列表中使用它会导致糟糕的性能。

You could use append, but beware that it can lead to bad performance if used in a loop or on very long lists.

(append '(1 2 3) (list (+ 2 2)))

如果性能很重要,通常的成语是由prepending(使用利弊<建筑列表/一>),然后反向(或 nreverse )。

If performance is important, the usual idiom is building lists by prepending (using cons), then reverse (or nreverse).

这篇关于什么是“利弊”将项目添加到列表的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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