Path: utzoo!attcan!uunet!know!samsung!usc!wuarchive!sdd.hp.com!ucsd!dog.ee.lbl.gov!hellgate.utah.edu!cdr.utah.edu!moore From: moore%cdr.utah.edu@cs.utah.edu (Tim Moore) Newsgroups: comp.lang.lisp Subject: Re: Lisp 1.5 functional values as objects Message-ID: <1990Oct10.100039.6974@hellgate.utah.edu> Date: 10 Oct 90 16:00:39 GMT References: <1950001@hpgnd.HP.COM> Organization: University of Utah CS Dept Lines: 60 In article raja@copper.ucs.indiana.edu (Raja Sooriamurthi) writes: >dave@hpgnd.HP.COM (Dave PENKLER) writes: > >>Is there any way to do this sort of thing in newer dialects of lisp ? > >This might be out of place in this news-group, but Scheme can handle >these sort of objects very elegantly. For instance a stack could be >defined as: > >(define make-stack > (lambda () ; if desired initial values could be passed here > (let ([stack 'any-initial-value]) > (lambda msg ; the function (object) that is returned > (case (car msg) > [push (set! stack (cons (cadr msg) stack))] > [pop (set! stack (cdr stack))] > [top (car stack)] > [show stack] > [flush (set! stack 'any-initial-value)] > [else (error 'stack "unknown method name")]))))) > >(define send > (lambda args > (apply (car args) (cdr args)))) > Scheme doesn't have a monopoly on this style of programming; Common Lisp (and any language with closures) can do the same thing: (defun make-stack () (let ((stack nil)) #'(lambda (cookie &optional val) (case cookie (push (push val stack)) (pop (pop stack)) (top (car stack)) (show stack) (flush (setq stack nil)) (t (error "unknown method name")))))) (defun send (object &rest args) (apply object args)) >In this style of creating objects, values can be shared between different >classes by means of delegation. > >Springer and Friedman devote an entire chapter to this mechanism of creating >objects in their book _Scheme and the art of Programming_ (MIT press >and McGraw Hill, 1989) Also, this technique is used in Abelson and Sussman, "Structure and Interpretation of Computer Programs", MIT press, 1985. Incidently, I feel that Common Lisp, with the exception of call/cc, has the same semantic power (and much more) of scheme. Any disagreement? (Warning, religious war ahead!) >- Raja Tim Moore moore@cs.utah.edu {bellcore,hplabs}!utah-cs!moore "Ah, youth. Ah, statute of limitations." -John Waters