Lisp - Tears of Joy Part 8
Lisp - Tears of Joy Part 8
Submit Tips
Search
HOME
REVIEWS
HOW-TOS
CODING
INTERVIEWS
FEATURES
OVERVIEW
BLOGS
SERIES
IT ADMIN
Search for:
Search
Lisp has been hailed as the worlds most powerful programming language. But only the top percentile of programmers use it because of its cryptic syntax and academic reputation. This is rather unfortunate, since Lisp isnt that hard to grasp. If you want to be among the crme de la crme, this series is for you. This is the eighth article in the series that began in June 2011.
In rare moments of self-reflection, when I allow myself to doubt my skills as a Lisp evangelist, I sometimes wonder if I have left behind some of my fellow programmers who favour the objectoriented style of programming. Just because I have been focusing on Lisp as a functional programming paradigm, it doesnt mean we dont have a role for you in our plans of world domination. Read on to know where you fit in.
Follow
+2,503
difficult to understand, especially if that data changes over time. Therefore, many Lispers prefer to use functional techniques over object-oriented techniques, though the two can often be used together with some care. Nonetheless, there are still many domains in which object-oriented techniques are invaluable, such as in user interface programming or simulation programming. On the other hand, James Hague, in his assessment of functional programming argues that, 100 per cent pure functional programming doesnt work. Even 98 per cent pure functional programming doesnt work. But if the slider between functional purity and 1980s BASIC-style imperative messiness is kicked down a few notches say to 85 per cent then it really does work. You get all the advantages of functional programming, but without the extreme mental effort and un-maintainability that increases as you get closer and closer to perfectly pure.
CLOS
If OO is what gets you going, Common Lisp offers the most sophisticated object-oriented programming framework of any major programming language. Its called Common Lisp Object System (CLOS). It is customisable at a fundamental level, using the Meta-Object Protocol (MOP). It has been claimed that theres really nothing like it anywhere else in programming. It lets you control incredibly complex software without losing control over the code. Let the tears of joy flow
Popular Comments Tag cloud
August 13, 2013 39 Comments Diksha P Gupta
used to execute operations on objects depends on the classes of the objects. The sub-class mechanism allows classes to be defined that share the structure and the behaviour of other classes. This sub-classing is a tool for modularisation of programs.
#5: The primary entities of the system are all first-class objects.
In the Common Lisp Object System, generic functions and classes are first-class objects with no intrinsic names. It is possible and useful to create and manipulate anonymous generic functions and classes. The concept of first-class is important in Lisp-like languages. A first-class object is one that can be explicitly made and manipulated; it can be stored in any location that can hold general objects.
Classes
The d e f c l a s smacro is used to define a new class. The definition of a class consists of its name, a list of its direct super-classes, a set of slot specifiers and a set of class options. The direct super-classes of a class are those from which the new class inherits structure and behaviour. When a class is defined, the order in which its direct super-classes are mentioned in the d e f c l a s sform defines a local precedence order on the class and those super-classes. The local precedence order is represented as a list consisting of the class, followed by its direct super-classes, in the order mentioned in the d e f c l a s sform. The following two classes define a representation of a point in space. The x y p o s i t i o nclass is a sub-class of the p o s i t i o n class:
>( d e f c l a s sp o s i t i o n( )( ) ) >( d e f c l a s sx y p o s i t i o n( p o s i t i o n ) ( ( x: i n i t f o r m0 ) ( y: i n i t f o r m0 ) ) ( : a c c e s s o r p r e f i xp o s i t i o n ) )
The p o s i t i o nclass is useful if we want to create other sorts of representations for spatial positions. The xand ycoordinates are initialised to 0 in all instances, unless explicit values are supplied for them. To refer to the x coordinate of an instance of x y p o s i t i o n , you would write:
>( p o s i t i o n xp o s i t i o n )
The macro d e f c l a s sis part of the Object System programmatic interface and, as such, is on the first of the three levels of the Object System.
Generic functions
The class-specific operations of the Common Lisp Object System are provided by generic functions and methods. A generic function is one whose behaviour depends on the classes or identities of the arguments supplied to it. The methods associated with the generic function define the class-specific operations of the generic function. Like an ordinary Lisp function, a generic function takes arguments, performs a series of operations and returns values. An ordinary function has a single body of code that is always executed when the function is called. A generic function is able to perform different series of
operations and to combine the results of the operations in different ways, depending on the class or identity of one or more of its arguments. Generic functions are defined by means of the d e f g e n e r i c o p t i o n sand d e f m e t h o dmacros. The d e f g e n e r i c o p t i o n smacro is designed to allow for the specification of properties that pertain to the generic function as a whole, and not just to individual methods. The d e f m e t h o d form is used to define a method. If there is no generic function of the given name, however, it automatically creates a generic function with default values for the argument precedence order (left-to-right, as defined by the lambda-list), the generic function class (the class s t a n d a r d g e n e r i c f u n c t i o n ), the method class (the class s t a n d a r d m e t h o d ) and the method
combination type (s t a n d a r d m e t h o dc o m b i n a t i o n ).
Methods
The class-specific operations provided by generic functions are themselves defined and implemented by methods. The class or identity of each argument to the generic function indicates which method or methods are eligible to be invoked. A method object contains a method function, an ordered set of parameter specialisers that specify when the given method is applicable, and an ordered set of qualifiers that are used by the method combination facility to distinguish between methods. The d e f m e t h o dmacro is used to create a method object. A d e f m e t h o dform contains the code that is to be run when the arguments to the generic function cause the method that it defines, to be selected. If a d e f m e t h o dform is evaluated, and a method object corresponding to the given generic function name, parameter specialisers and qualifiers already exists, then the new definition replaces the old. Generic functions can be used to implement a layer of abstraction on top of a set of classes. For example, the x y p o s i t i o nclass can be viewed as containing information in polar coordinates. Two methods have been defined p o s i t i o n r h oand p o s i t i o n t h e t a , that calculate the and coordinates given an instance of x y p o s i t i o n :
>( d e f m e t h o dp o s i t i o n r h o( ( p o sx y p o s i t i o n ) ) ( l e t( ( x( p o s i t i o n xp o s ) ) ( y( p o s i t i o n yp o s ) ) ) ( s q r t( +( *xx )( *yy ) ) ) ) ) >( d e f m e t h o dp o s i t i o n t h e t a( ( p o sx y p o s i t i o n ) ) ( a t a n( p o s i t i o n yp o s )( p o s i t i o n xp o s ) ) )
It is also possible to write methods that update the virtual slots p o s i t i o n r h oand p o s i t i o n t h e t a :
>( d e f m e t h o d s e t fp o s i t i o n r h o( ( p o sx y p o s i t i o n ) )( r h o ) ( l e t *( ( r( p o s i t i o n r h op o s ) ) ( r a t i o( /r h or ) ) ) ( s e t f( p o s i t i o n xp o s )( *r a t i o( p o s i t i o n xp o s ) ) ) ( s e t f( p o s i t i o n yp o s )( *r a t i o( p o s i t i o n yp o s ) ) ) ) ) >( d e f m e t h o d s e t fp o s i t i o n t h e t a( ( p o sx y p o s i t i o n ) )( t h e t a ) ( l e t( ( r h o( p o s i t i o n r h op o s ) ) ) ( s e t f( p o s i t i o n xp o s )( *r h o( c o st h e t a ) ) ) ( s e t f( p o s i t i o n yp o s )( *r h o( s i nt h e t a ) ) ) ) )
This is precisely the same syntax that would be used if the positions were explicitly stored as polar coordinates.
Class redefinition
The Common Lisp Object System provides a powerful class-redefinition facility. When a d e f c l a s sform is evaluated, and a class with the given name already exists, the existing class is redefined. Redefining a class modifies the existing class object to reflect the new class definition. You may define methods on the generic function c l a s s c h a n g e dto control the class redefinition process. This generic function is invoked automatically by the system after d e f c l a s shas been used to redefine an existing class; for example, suppose it becomes apparent that the application that requires representing positions uses polar coordinates more than it uses rectangular coordinates. It might make sense to define a sub-class of p o s i t i o nthat uses polar coordinates:
>( d e f c l a s sr h o t h e t a p o s i t i o n( p o s i t i o n ) ( ( r h o: i n i t f o r m0 )
( t h e t a: i n i t f o r m0 ) ) ( : a c c e s s o r p r e f i xp o s i t i o n ) )
Inheritance
Inheritance is the key to program modularity within CLOS. A typical object-oriented program consists of several classes, each of which defines some aspect of behaviour. New classes are defined by including the appropriate classes as super-classes, thus gathering the desired aspects of behaviour into one class. In general, slot descriptions are inherited by sub-classes. That is, slots defined by a class are usually slots implicitly defined by any sub-class of that class, unless the sub-class explicitly shadows the slot definition. A class can also shadow some of the slot options declared in the
d e f c l a s sform of one of its super-classes by providing its own description for that slot.
A sub-class inherits methods in the sense that any method applicable to an instance of a class is also applicable to instances of any sub-class of that class (all other arguments to the method being the same). The inheritance of methods acts the same way regardless of whether the method was created by using d e f m e t h o dor by using one of the d e f c l a s soptions that cause methods to be generated automatically. I hope with this article I have managed to convince OO programmers that Lisp is generous enough to cater to your style of thinking. Stick with me, and I promise that you wont be disappointed. So far weve seen how to fit the nuts and bolts into the engine. Next month, well learn how to paint it a nice shiny red I am referring to Graphical Programming in Lisp!
References
Let Over Lambda, Doug Hoyte CLOS: Integrating Object-Oriented and Functional programming, Richard P. Gabriel, Jon L White, Daniel G. Bobrow
Related Posts:
Lisp: Tears of Joy, Part 4 Lisp: Tears of Joy, Part 10 Lisp: Tears of Joy, Part 7 Lisp: Tears of Joy, Part 5 Loading Library Files in C++
Tags: CLOS, Common Lisp, Common Lisp Object System, Conard Barski, control functions, Daniel G. Bobrow, Doug Hoyte, flow control, functional programming, James Hague, Jon L White, LFY January 2012, Lisp, Lisp: Tears of Joy series, Michael Feathers, object interfaces, object-oriented programming, oo approach, oo programming, programming, programming style, recursion, Richard P Gabriel
Previous Post
Next Post
Device Drivers, Part 14: A Dive Inside the Hard Disk for Understanding Partitions
Reviews
How-Tos
Coding
Interviews
Features
Overview
Blogs
Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems
All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.