列表作为通用数据类型表示有什么缺点?

发布于 01-01 15:32 字数 98 浏览 6 评论 0原文

Lisp 程序员倾向于使用列表来表示所有其他数据类型。

但是,我听说列表并不是数据类型的良好通用表示。

与使用记录相比,以这种方式使用列表有什么缺点?

Lisp programmers tend to use lists to represent all other data types.

However, I have heard that lists are not a good universal representation for data types.

What are the disadvantage of lists being used in this manner, in contrast to using records?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓2025-01-08 15:32:17

你提到“记录”。我认为您指的是固定元素结构/对象/复合数据。例如,在 HtDP 语法中:

;; a packet is (make-packet destination source text) where destination is a number, 
;; source is a number, and text is a string.

...并且您询问将数据包表示为长度为 3 的列表的优缺点,
而不是作为一段复合数据(或“记录”)。

在适合复合数据的情况下(这些值具有特定的角色和名称,并且它们的数量是固定的),复合数据通常是更可取的;它们帮助您捕获程序中的错误,这是编程的必要条件。

You mention "record". By this I take it that you're referring to fixed-element structs/objects/compound data. For instance, in HtDP syntax:

;; a packet is (make-packet destination source text) where destination is a number, 
;; source is a number, and text is a string.

... and you're asking about the pros and cons of representing a packet as a list of length three,
rather than as a piece of compound data (or "record").

In instances where compound data is appropriate--the values have specific roles and names, and there are a fixed number of them--compound data is generally preferable; they help you to catch errors in your programs, which is the sine qua non of programming.

栖竹2025-01-08 15:32:17

缺点是它通用。有时这与性能相关:您需要恒定时间的查找(数组、哈希表)。有时,这与组织相关:您想要命名数据位置(哈希表、记录......尽管您可以在列表中使用名称、值对)。让代码自我记录需要作者付出一点努力(比记录更努力)。有时,您希望类型系统能够捕获由于将内容放在错误位置(记录、类型化元组)而造成的错误。

但是,大多数问题都可以通过 OptimizeLater 解决。列表是一种通用的小数据结构。

The disadvantage is that it isn't universal. Sometimes this is performance related: you want constant time lookups (array, hash table). Sometimes this is organization related: you want to name your data locations (Hash table, record ... although you could use name,value pairs in the list). It requires a little diligence on the part of the author to make the code self-documenting (more diligence than the record). Sometimes you want the type system to catch mistakes made by putting things in the wrong spot (record, typed tuples).

However, most issues can be addressed with OptimizeLater. The list is a versatile little data structure.

兔姬2025-01-08 15:32:17

您正在谈论 Peter Seibel 在 Practical Common Lisp 第 11 章中提到的内容:

[开始] Lisp 集合的讨论
与列表。 。 。常常导致读者误入歧途
结论是列表是 Lisp 唯一的集合类型。为了搞事情
更糟糕的是,因为 Lisp 的列表是一种非常灵活的数据结构,所以
可以将它们用于数组和哈希表的许多用途
用于其他语言中。但过于关注是错误的
列表;虽然它们是表示 Lisp 的关键数据结构
代码就像 Lisp 数据一样,在很多情况下其他数据结构更重要
合适。

一旦您熟悉了 Common Lisp 提供的所有数据类型,
您还将看到列表对于数据原型设计很有用
稍后将被更有效的结构所取代
一旦明确了数据的具体使用方式。

我认为的一些原因是:

  • 例如,大型哈希表比等效的 alist 具有更快的访问速度
  • 单一数据类型的向量更紧凑,并且访问速度更快
  • 向量可以通过索引更有效且更轻松地访问
  • 对象和结构允许您按名称而不是位置访问数据

归根结底是为手头的任务使用正确的数据类型。当它不明显时,你有两个选择:猜测并稍后修复,或者现在弄清楚;有时,这两种方法都是正确的方法。

You're talking about what Peter Seibel addresses in Chapter 11 of Practical Common Lisp:

[Starting] discussion of Lisp's collections
with lists . . . often leads readers to the mistaken
conclusion that lists are Lisp's only collection type. To make matters
worse, because Lisp's lists are such a flexible data structure, it is
possible to use them for many of the things arrays and hash tables are
used for in other languages. But it's a mistake to focus too much on
lists; while they're a crucial data structure for representing Lisp
code as Lisp data, in many situations other data structures are more
appropriate.

Once you're familiar with all the data types Common Lisp offers,
you'll also see that lists can be useful for prototyping data
structures that will later be replaced with something more efficient
once it becomes clear how exactly the data is to be used.

Some reasons I see are:

  • A large hashtable, for example, has faster access than the equivalent alist
  • A vector of a single datatype is more compact and, again, faster to access
  • Vectors are more efficiently and easily accessed by index
  • Objects and structures allow you to access data by name, not position

It boils down to using the right datatype for the task at hand. When it's not obvious, you have two options: guess and fix it later, or figure it out now; either of those is sometimes the right approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文