Foop, the movie
Posted: Sat Dec 08, 2007 3:33 pm
Friends and Fans of newLISP
http://www.newlispfanclub.alh.net/forum/
http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=2&t=2041
Code: Select all
#!/usr/bin/newlisp
; newlisp 2.9.7 - dec 2007
; This script creates objects 'Atom' and 'Ion'.
; A ion is just an altered atom. So the class 'Ion'
; is derived from the class 'Atom'and inherits from this one
; all its attributes and methods, appending its own.
; One of these added methods (the method 'display') replaces
; a method of the same name inherited from 'Atom'.
; Then 'Atom' and 'Ion' have both a method with the same name
; but making different work. That is polymorphism.
(set 'table '(nil
("hydrogen" 0)("helium" 2)("lithium" 4)
("beryllium" 5)("boron" 6)("carbon" 6)
("nitrogen" 7)("oxygen" 8)("fluorine" 10)("neon" 10)))
(define (Atome:Atome nat , np ne nn)
; nat=atomic number
(set 'np nat 'ne nat 'nn (table nat 1))
(list Atome nat np ne nn))
(define (Atome:display a)
(println "\nName of the element : " (table (a 2) 0))
(println (a 2) " protons, " (a 3) " electrons, " (a 4) " neutrons"))
(new Atome 'Ion)
(define (Ion:Ion nat charge , np ne nn)
(set 'np nat 'ne (- nat charge) 'nn (table nat 1))
(list Ion nat np ne nn charge))
(define (Ion:display a)
(Atome:display a)
(println "Electrified particle. Charge = " (a 5)))
;; Main Program
(set 'a1 (Atome 5))
(set 'a2 (Ion 3 1))
(set 'a3 (Ion 8 -2))
(:display a1)
(:display a2)
(:display a3)
Fine and funny :Dcormullion wrote:And when you mention elements in the periodic table just after I've watched michael's wonderful animated movie, I can't resist pointing you to this wonderful flash animation:
http://www.privatehand.com/flash/elements.html
Code: Select all
(define (Atome:Atome nat)
; nat=atomic number
(list Atome nat nat nat (table nat 1)))
Code: Select all
(define (Ion:Ion nat charge)
;; (Ion nat np ne nn charge)
(list Ion nat nat (- nat charge) (table nat 1) charge))
Yes, it's confusing this non-OOPer too. If I understand correctly: you're not able to modify the 'internal variables' of an object instance using these method calls. So when you want to, say, update the x and y coordinates of an object, you have to create a new copy of it with the modified values. Why can't you change the values in place, then...?m i c h a e l wrote: I believe the concept of changeless objects is the part of FOOP that will give them the most difficulty.
well, not continually, just a bit more, but in the overall picture the difference is probably less than 5%, because so much copying is going on in the interpreter internally anyway.Now you're continually making copies of the data...
I've done that quickly from a Python script and I didn't take time to think about the name of the attributes that I copied out again from the original script. But I do note your remarks...m i c h a e l wrote:
I do have a question about your constructors. Since the values of the symbols np, ne, nn, etc. are lost with the completion of the constructor, why not this?
Code: Select all
(define (Atome:Atome nat) ; nat=atomic number (list Atome nat nat nat (table nat 1)))
Or if you want to document the attributes:
Code: Select all
(define (Ion:Ion nat charge) ;; (Ion nat np ne nn charge) (list Ion nat nat (- nat charge) (table nat 1) charge))
There is no single, fixed way to do FOOP, so go with the way that feels most natural to you.