need help with Defun (not as simple as it sounds)

Discussion of Common Lisp
Post Reply
8mike
Posts: 3
Joined: Sun Feb 20, 2011 8:45 am

need help with Defun (not as simple as it sounds)

Post by 8mike » Sun Feb 20, 2011 9:08 am

Hi everybody and thanks in advance for your support

I'm making a university project in Lisp. The problem I have is that I don't know how to return the name of a function.
Let me explain

My project should be a Object Oriented Lisp, and I made everything. the only thing that doesn't work is method.
When I define a new class it has methods in it.
When I create a new instance of that class (for exapmple S1) I can make a new method in it which is a definition of a non existent method (for example a method named TALK). If I try to invoke that method with the name of that instance everything works (TALK S1 > "Hi, i'm S1"). If I create another instance (S2) with a method with the same name of TALK but with different implementation (for example TALK S2 > "My name is S2") it overwrites the other TALK, so if I try to invoke TALK S1 > "My name is S1".

In my project, everytime a function is defined it is not immediately defined with its true body, but with a standard body I defined. As soon as I invoke the function with the instance, the body should do spomething like this:

check if this method is defined in this instance or in his classes. if not then ERROR, else define that method with the body he finds in the instance and execute it; then redefine with the standard body. when I use this body I need the instance name (which is passed as soon as I invoke the method, ex TALK S1) but i also need the name of the function where the body is loaded, so i can check if it's defined in the instance or in his classes. But how can i do it with the need to write
talk 'talk s1 ?

I want to write
talk s1

and the program should do

>check function-name instance

where function-name is 'talk and instance is 's1
but it should be able to do the same if i write

>test-method q1

where here function-name is 'test-method and instance is 'q1

I hope i explained myself well, despite my bad english.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: need help with Defun (not as simple as it sounds)

Post by gugamilare » Sun Feb 20, 2011 2:07 pm

If I understood correctly, you created a Lisp dialect and you are now trying to define methods. It seems that you want to create methods on instances, not only on classes.

I believe there is more than one way to proceed. For instance, one possibility is to create an extra slot (field) in every class you create. This slot (which will be named, say, INSTANCE-METHODS) should contain an alist or hashtable with the methods defined for each instance. Then, for example, when you create the method TALK for S1, it would create the function

Code: Select all

(lambda (x)
  "Hi, I'm S1")
(or something like that) and put this function with the name of the method (in this case, TALK) into the slot INSTANCE-METHODS of S1, like this:

Code: Select all

(push (cons 'talk
        (lambda (x)
          "Hi, I'm S1"))
  (instance-methods s1))
Then, a function named TALK would also be created (or superseded), it should look more or less like this:

Code: Select all

(defun talk (instance)
  (let ((method (cdr (assoc 'talk (instance-methods instance)))))
    (if method
      (funcall method instance)
      ;; look for methods defined for the class of INSTANCE, otherwise signal an error
      )))
This is obviously not clean or efficient, but it is a start. I hope my explanation is easy to understand, I'm not sure I can simplify it without knowing more details about your Object Oriented Lisp.

Good luck with your project. Not many people choose to create a language as a project ;)

8mike
Posts: 3
Joined: Sun Feb 20, 2011 8:45 am

Re: need help with Defun (not as simple as it sounds)

Post by 8mike » Sun Feb 20, 2011 2:36 pm

thanks for the reply, I'm going to check it as soon as possible and then I'll tell if I done well

8mike
Posts: 3
Joined: Sun Feb 20, 2011 8:45 am

Re: need help with Defun (not as simple as it sounds)

Post by 8mike » Thu Feb 24, 2011 3:44 pm

Thank you very much, you saved my life, it worked perfectly!

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: need help with Defun (not as simple as it sounds)

Post by marcoxa » Fri Feb 25, 2011 9:48 am

Of course *that* works. I'll just have to update my tests! :lol: :mrgreen: :twisted:

MA
Marco Antoniotti

Post Reply