什么是C结构的Lisp语言的习惯相同呢? [英] What's the idiomatic equivalent of C structs in Lisp?

查看:170
本文介绍了什么是C结构的Lisp语言的习惯相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C型语言,有较强的重点从一开始,并在每入门书结构/记录和对象。然后,他们的整个系统都是围绕这样的管理结构,它们之间的相互关系和继承。

In C-type languages, there is a strong emphasis on structs/records and objects from the very beginning and in every introductory book. Then, their complete systems are designed around managing such structs, their mutual relations and inheritance.

在Lisp中的文档,通常可以找到1-2页有关如何Lisp的也有defstruct,一个简单的例子,和多数民众赞成它通常。此外,结构嵌套从未被提及。

In Lisp documentation, you can usually find 1-2 pages about how Lisp "also" has a defstruct, a simple example, and thats usually it. Also, nesting of structures is never mentioned at all.

有关有人从C背景的,它首先似乎是举办不同的数据类型层次心不是在Lisp中的preferred方法,但除了CLOS,这是一个完全成熟的对象,系统太复杂,如果你只想结构,而且除了craming一切都变成清单,心不是一个明显的方式来转移你的C结构的知识。

For someone coming from a C background, it first seems that organizing different data types hierarchically isnt the preferred method in Lisp, but apart from CLOS, which is a full blown object system and too complicated if you just want structs, and apart from craming everything into lists, there isnt an apparent way to transfer your C struct knowledge.

什么是分级组织大多数类似C的结构数据的惯用方式Lisp的?

What is the idiomatic Lisp way of hierarchically organizing data which most resembles C structs?

-

我认为总结回答我的问题是:对于初学者学习的目的,defstruct和/或Plist档案,虽然传统特色,就可以使用,因为他们最接近C的结构,但他们已被largerly取代通过更灵活的defclass / CLOS,其中大多数Lisp程序目前使用。

I think the summary answer to my question would be: For beginner learning purposes, defstruct and/or plists, although "legacy features", can be used, since they most closely resemble C structs, but that they have been largerly superseded by the more flexible defclass/CLOS, which what most Lisp programs use today.

这是我的第一个SO问题,所以感谢大家的时间来回答吧。

This was my first question on SO, so thanks everyone for your time answering it.

推荐答案

使用CLOS。它并不复杂。

否则,使用结构。

如果你有一个具体的问题如何使用它们,然后就问我。

If you have a specific question how to use them, then just ask.

(defclass point ()
  ((x :type number)
   (y :type number)))

(defclass rectangle ()
  ((p1    :type point)
   (p2    :type point)
   (color :type color)))

之类的东西最终会导致像矩形在CLIM 接口(该Common Lisp的界面管理器)。

Stuff like that eventually leads to interfaces like Rectangles in CLIM (the Common Lisp Interface Manager).

记录

要在其上扩展了一下:已在一些低级别的情况下被使用历史的结构。结构具有单一继承和插槽存取'快'。一些Lisp的方言有比Common Lisp中提供了更多的结构。然后从各种形式的面向对象的重新presentations的70年代中期已经开发了用于Lisp的。大多数结构化对象的重新presentation从结构转移到某种面向对象的Lisp扩展。上世纪80年代期间,流行就像香料,循环和其它基于类的系统。基于帧的或基于原型的系统,如KEE单位或对象Lisp中也很受欢迎。第一台Macintosh Common Lisp中使用的对象的Lisp其所有的用户界面和IO设备。麻省理工学院的Lisp机采用香精基本上无处不在。在80年代中期ANSI CL开始开发的。一个常见的​​面向对象的系统的开发特别是对Common Lisp的:CLOS。它是基于口味和循环。在那段时间里大多没有采取任何真正改善结构 - 除了实现者设法提高执行并提供一个浅浅的CLOS整合。例如结构不提供数据的任何填料。如果有4个字节内容的两个插槽,没有办法指示Common Lisp的为en code两个插槽成一个8位的内存区域。

To expand on it a bit: Historically 'structures' have been used in some low-level situations. Structures have single inheritance and slot access is 'fast'. Some Lisp dialects have more to structures than what Common Lisp offers. Then from the mid-70s on various forms of object-oriented representations have been developed for Lisp. Most of the representation of structured objects moved from structures to some kind of object-oriented Lisp extension. Popular during the 80s were class-based systems like Flavors, LOOPS and others. Frame-based or prototype-based systems like KEE Units or Object Lisp were also popular. The first Macintosh Common Lisp used Object Lisp for all its UI and IO facilities. The MIT Lisp machine used Flavors basically everywhere. Starting in the mid 80s ANSI CL was developed. A common OO-system was developed especially for Common Lisp: CLOS. It was based on Flavors and Loops. During that time mostly nothing was done to really improve structures - besides implementors finding ways to improve the implementation and providing a shallow CLOS integration. For example structures don't provide any packing of data. If there are two slots of 4 bytes content, there is no way to instruct Common Lisp to encode both slots into a single 8 bit memory region.

作为一个例子,你可以在 Lisp机器手册,对结构章节(PDF)请参阅,它有比Common Lisp中提供了更为复杂的结构。有些是已经在70美元Maclisp p $ psent: DEFSTRUCT在Maclisp手动

As an example you can see in the Lisp Machine Manual, chapter on structures (PDF), that it had much more complex structures than what Common Lisp provides. Some of that was already present in Maclisp in the 70s: DEFSTRUCT in the Maclisp manual.

CLOS中,Common Lisp的对象系统

大多数人会同意,CLOS是一个不错的设计。这有时会导致'放大'code,主要是因为标识符可以长期做下去。但是,有一些CLOS code,就像一个在AMOP书,那是真的写得很好,并显示它应该如何被使用。

Most people would agree that CLOS is a nice design. It sometimes leads to 'larger' code, mostly because identifiers can get long. But there is some CLOS code, like the one in the AMOP book, that is really nicely written and shows how it is supposed to be used.

随着时间的推移实现者不得不面对开发商想用CLOS,也希望有结构的速度的挑战。这更是与'全'CLOS,其中包括CLOS几乎标准的元对象协议(MOP)的任务。所以有一些技巧,实现者提供。在80年代一些软件中使用的开关,因此它可能使用的结构或使用CLOS编译 - CLX(低级常见Lisp的X11接口是一个例子)。其原因是:在某些计算机和实现CLOS比结构慢得多。今天这将是不寻常的是提供这样一个编译开关

Over time implementors had to deal with the challenge that developers wanted to use CLOS, but also wanted to have the 'speed' of structures. Which is even more a task with the 'full' CLOS, which includes the almost standard Meta Object Protocol (MOP) for CLOS. So there are some tricks that implementors provide. During the 80s some software used a switch, so it could compiled using structures or using CLOS - CLX (the low-level Common Lisp X11 interface was an example). The reason: on some computers and implementations CLOS was much slower than structures. Today it would be unusual to provide such a compilation switch.

如果我在一个良好的Common Lisp实现今天看起来,我希望它使用CLOS几乎无处不在。流CLOS类。条件是CLOS类。图形用户界面工具包采用CLOS类。该编辑器使用CLOS。它甚至可能会对外类(比如说,目标C类)融入CLOS。

If I look today at a good Common Lisp implementation, I would expect that it uses CLOS almost everywhere. STREAMs are CLOS classes. CONDITIONs are CLOS classes. The GUI toolkit uses CLOS classes. The editor uses CLOS. It might even integrate foreign classes (say, Objective C classes) into CLOS.

在任何非玩具Common Lisp实现CLOS将提供结构化的数据,一般性的行为和一堆其他东西的工具。

In any non-toy Common Lisp implementation CLOS will be the tool to provide structured data, generic behavior and a bunch of other things.

正如在一些其他的答案在一些地方可能并不需要CLOS提及

As mentioned in some of the other answers, in some places CLOS might not be needed.

Common Lisp的可以从一个函数返回多个值:

Common Lisp can return more than one value from a function:

(defun calculate-coordinates (ship)
   (move-forward ship)
   (values (ship-x ship)
           (ship-y ship)))

人们可以在关闭存储数据:

One can store data in closures:

(defun create-distance-function (ship x y)
   (lambda ()
     (point-distance (ship-x ship) (ship-y ship) x y)))

有关配置可以使用某种类型的列表:

For configuration one can use some kind of lists:

(defship ms-germany :initial-x 0 :initial-y 0)

您可以打赌,我会实现CLOS船模。

You can bet that I would implement the ship model in CLOS.

从编写和维护软件CLOS的教训是,它需要精心设计和CLOS是如此强大,一个可以创建它真的复杂的软件 - 一个复杂而这往往不是一个好主意。重构和简化!幸运的是,对于许多任务的基本CLOS设施足以:DEFCLASS,DEFMETHOD和MAKE-INSTANCE

A lesson from writing and maintaining CLOS software is that it needs to be carefully designed and CLOS is so powerful that one can create really complex software with it - a complexity which is often not a good idea. Refactor and simplify! Fortunately, for many tasks basic CLOS facilities are sufficient: DEFCLASS, DEFMETHOD and MAKE-INSTANCE.

指针到CLOS介绍

一开始,理查德P.加布里埃尔有他 CLOS论文的下载。

For a start, Richard P. Gabriel has his CLOS papers for download.

另请参见:

这篇关于什么是C结构的Lisp语言的习惯相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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