Scheme中的继承类
现在我研究OOP-Scheme的一部分。我可以像这样在Scheme中定义类:(
(define (create-queue)
(let ((mpty #t)
(the-list '()))
(define (enque value)
(set! the-list (append the-list (list value)))
(set! mpty #f)
the-list)
(define (deque)
(set! the-list (cdr the-list))
(if (= (length the-list) 0)
(set! mpty #t))
the-list)
(define (isEmpty)
mpty)
(define (ptl)
the-list)
(define (dispatch method)
(cond ((eq? method 'enque) enque)
((eq? method 'deque) deque)
((eq? method 'isEmpty) isEmpty)
((eq? method 'print) ptl)))
dispatch))
我可以在Scheme中实现类继承吗?
Now I research OOP-part of Scheme. I can define class in Scheme like this:
(define (create-queue)
(let ((mpty #t)
(the-list '()))
(define (enque value)
(set! the-list (append the-list (list value)))
(set! mpty #f)
the-list)
(define (deque)
(set! the-list (cdr the-list))
(if (= (length the-list) 0)
(set! mpty #t))
the-list)
(define (isEmpty)
mpty)
(define (ptl)
the-list)
(define (dispatch method)
(cond ((eq? method 'enque) enque)
((eq? method 'deque) deque)
((eq? method 'isEmpty) isEmpty)
((eq? method 'print) ptl)))
dispatch))
(Example from css.freetonik.com)
Can I implement class inheritance in Scheme?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我不会称其为类,但这就是我。这只是闭包和原始方案。
方案本身没有对象系统。然而,Scheme 能够实现类系统。
如果您想使用 oop 系统,您可以尝试一些为Scheme编写的系统。
这里是一个列出了几个的链接,当然还有其他的。
Well, I wouldn't call that a class, but that's just me. That's simply closures and raw scheme.
Scheme itself does not have an object system. However, Scheme is capable of implementing a class system.
If you would like to use an oop system, there are several written for Scheme that you can try.
Here is a link that lists several, there are certainly others.
OOP 语言使用继承来模拟多态性,即创建可以响应已发布的消息列表的对象类。您可以在没有显式继承的情况下在Scheme中拥有多态性,因为它是一种动态类型语言。比较 Java 中“Animal”类的实现和它在 Scheme 中的相应实现:
现在,Scheme 中使用闭包的相应实现:
事实上,只有当我们必须处理太多私有状态时,我们才需要闭包。 Scheme 提供了许多方法来模拟上面的简单“类层次结构”。下面是我们开发一个小型“消息调度”工具的一种方法,我们可以在对象列表上使用它:
Scheme 提供的功能抽象机制足够强大,使我们不必担心完整的对象系统。尽管如此,Scheme 仍然有一些对象系统。看看 Tiny-CLOS (基于 CLOS)。 Lisp in Small Pieces一书讨论了对象的实现System for Scheme(基于 Meroon)。
OOP languages use inheritance to simulate polymorphism, i.e to create a class of objects that can respond to a published list of messages. You can have polymorphism in Scheme without explicit inheritance because it is a dynamically typed language. Compare the implementation of an "Animal" class in Java and its corresponding implementation in Scheme:
Now the corresponding implementation in Scheme using closures:
In fact we need closures only if we have to deal with too much private state. Scheme provide many ways to simulate simple "class hierarchies" like the above. Here is one method in which we develop a tiny "message dispatching" facility which we can use on a list of objects:
The functional abstraction mechanisms provided by Scheme is powerful enough to make us not worry about a full object system. Still, there are some object systems for Scheme. Have a look at Tiny-CLOS (based on CLOS). The book Lisp in Small Pieces discuss an implementation of an Object System for Scheme (based on Meroon).
如果您决定确实想要推出自己的 OOP 系统,那么您当然可以在Scheme 中实现继承。这样做的一种方法是关闭所需超类的实例,该实例是在创建派生类的实例时“构造”的,并在
dispatch
过程中添加一个额外的子句,如下所示还允许轻松重写方法,如示例所示。
当然,这是相当乏味的,并且涉及大量的样板文件。你可以用宏让它变得更愉快,但如果你有兴趣在Scheme中实际进行OOP而不是作为学习练习进行实验,我支持Will Hartung的建议使用Scheme的许多现有对象系统之一。
You certainly can implement inheritance in Scheme, if you decide that you do want to roll your own OOP system. One way of doing so is to close over an instance of the desired superclass which is "constructed" when you make the instance of the derived class, and have an extra clause in the
dispatch
procedure, something likeThis also allows easy overriding of methods, as shown in the example.
Of course, this is pretty tedious, and involves plenty of boilerplate. You could make it more pleasant with macros, but if you're interested in actually doing OOP in Scheme instead of experimenting as a learning exercise, I second Will Hartung's suggestion to use one of the many existing object systems for Scheme.
PLT 方案中有一个相当完善的类系统:
http://docs.plt- schema.org/guide/classes.html
There's a pretty fully-developed class system in PLT Scheme:
http://docs.plt-scheme.org/guide/classes.html