Changeset 21940 in project

12/13/10 11:32:58 (10 years ago)
Christian Kellermann

gazette: #16 intermediate state of the recipe. Parental advisory missing

1 edited


  • gazette/src/issues/

    r21938 r21940  
    1313[[egg:lookup-table|lookup-table egg]].  [[|Alan Post]]
    1414has been busy getting his [[egg:genturfahi|genturfa'i egg]] into
    15 shape. As well as [[user:mario-domenech-goulart|Mario Goulart]]
    16 clarifying the [[egg:awful|awful]] egg documentation.
     15shape. [[user:mario-domenech-goulart|Mario Goulart]] has ported the
     16[[egg:statvfs|statvfs egg]] to chicken 4.
    1818== 2. Core development
    4848But alas, this has also been bug finding week! [[|Alan
    4949Post]] as well as Moritz and Peter stumbled over some strange findings
    50 during the last week. Alan's [[egg:genturfa'i|genturfa'i]] egg somehow
     50during the last week. Alan's [[egg:genturfahi|genturfa'i]] egg somehow
    5151caused the compiler to create huge amounts of code as you know from
    5252[[issues/15.html|last week's]] issue.
    5858If you are interested in the process of this issue follow
    59 [[|Ticket #440]].
     59[[|ticket #440]].
    6161[[user:sven-hartrumpf|Sven Hartrumpf]] pointed out a problem in the
    7777== 4. Omelette Recipes
     79For this issue I want to take you on a tour through chicken scheme's
     80object system [[egg:coops|coops]]. It is the successor of the older
     81and now unsupported [[egg:tinyclos|tinyclos]] extension. So if you
     82have been relying on tinyclos you should consider switching to coops.
     84Now I can hear some of you say: "Wait, an object system for a
     85funcitonal language?". Yes! Scheme is much more than a functional
     86language, it is a ''multi-paradigm'' language meaning that you can
     87choose the programming paradigm to fit your problem. Functional,
     88imperative and object oriented, all is possible with scheme.
     90The main goal of objects is to hide (state) information from its
     91users. You should not be concerned with the nitty gritty
     92implementation details of a function (and not messing with it!), thus
     93objects provide a clean interface to your program. This interface is
     94used by sending messages in one form or another to your object.  Not
     95covered by this definiton of object orientation is the notion of a
     96'class hierarchy'' and ''inheritance''. Languages like C++ and Java
     97heavily rely on this paradigm. In this discussion classes are general
     98plans of how objects will look like, while objects are the "real
     99thing". Some people call objects instances of classes.
     101In scheme you can already implement objects (without classes) using a
     102dispatching method and you may have already seen the famous bank
     103account example by Abelson / Sussman in Structures and Interpretation
     104of Computer Programs. If not you can look it up in
     1063.1.1]]. Here the implementation details are hidden inside the
     107dispatch procedure.
     109Java and C++ also take information hiding to the extreme. Not only the
     110object's state is hidden behind walls but also the state manipulating
     111code, which is called ''methods''. What this means in practise is that
     112you need to assign a method to an object while planning your
     113abstractions. In most cases this works well. In others it will become
     114a deliberate design decision where to put a particular method.
     116A short example: Imagine you have a class ''human'' which is inherited
     117by a class ''male'' and ''female''. Now as we all have an urge to
     118reproduce where to put a method for having sex? Create a method
     119''haveSex'' in the human class, duplicate it in ''male'' or
     120''female''? What would the argument to such a class be? What about
     121having sex with people of the same sex, toys, animals, buildings...
     123Depending on your willingness to imagine all these things you will
     124have to make a deliberate design decision here. One that will affect
     125on how you see the world.
     127With scheme being a lisp you are in for a surprise: Its designers have
     128taken a slightly different approach to object orientation: a dynamic
     129object system centered around objects, classes and ''generic
     130functions''. Mostly these systems have been inspired by the ''Common
     131Lisp Object System (CLOS)''. Coops is no exceptions. Ok, we do know
     132what objects and classes are but what are generic functions?
     134First of all classes in coops look like this:
     136<enscript highlight="scheme">
     137(define-class <human> () name birthdate children)
     138(define-class <male> (<human>) testosteron-level)
     139(define-class <femals> (<human>) estrogen-level)
     142By tradition class names are encapsuled in ''<'' and ''>'' to
     143distinguish them form ordinary symbols.
     145As you can see coops' classes only hold on to their precious state. Methods
     146are defined ''separately'' from their classes. So for our human
     147example we declare a generic function called ''have-sex!'' and define
     148the methods for each class combination we want to imagine separately!
     150<enscript highlight="scheme">
     151(define-generic (have-sex! a b))
     153; the most common case
     154(define-method (have-sex! (male a) (female b))
     155               (let ((newborn (make-human "Bobby" (current-seconds) '())))
     156                    (set! (slot-value a children) (cons newborn (slot-value a children))))
     157                    (set! (slot-value a children) (cons newborn (slot-value a children))))
     159; all other cases (greatly simplified!)
     161(define-method (have-sex! (male a) (male b) 'just-fun))
     162(define-method (have-sex! (female a) (female b) 'just-fun))
     164; even more cases are up to your imagination....
     167So you can see here that we can provide sex recipies independently to
     168the kind (and number) of participants. We could even change and extend
     169our repertoire later should the need arise.
     171Now to a more theoretical example with less tendency to need parental
     172discretion: a game. Inspired by lisp tutorials we will see how the
     173coops object model can be used to create a small work of interactive
    79177== 5. About the Chicken Gazette
Note: See TracChangeset for help on using the changeset viewer.