¿Cuál es la desventaja de la lista como representación de tipo de datos universal?

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?

preguntado el 31 de enero de 12 a las 16:01

This question is much too general. -

I am a Lisp programmer, and I do not tend to use lists to represent other data types. I have not heard of others that do. What gave you that idea? -

I use hash tables, objects, strings, and vectors in my Lisp. And lists, too. I have no idea what sort of context you're talking in. I know that I use similar mixes of datatypes in Python and Perl. Please review modern Common Lisp instead of Lisp circa 1964. :-/ -

3 Respuestas

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.

Respondido el 31 de enero de 12 a las 21:01

The disadvantage is that it no es 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.

Respondido el 31 de enero de 12 a las 20:01

"However, most issues can be addressed with OptimizeLater. The list is a versatile little data structure." Well, I've worked at Lisp shops, and this "OptimizeLater" more often than not turned out to be "write the crappy list-based version crappily, quit the company, and then poor sacundim gets called at 12AM to rewrite the incomprehensible list-based code because it makes the application fail to scale." - Luis Casillas

Poor sacundim, indeed. :) I thought about that before I wrote it. As Svante stated in his comment, this isn't how you would typically write code if you had any experience. It is, however, how lisp textbooks generally start out. Hopefully plmday moves beyond the first few chapters in these books, when additional data structures are inevitably discussed. - ccoakley

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.

Respondido 03 Feb 12, 01:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.