Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Newbie question Common Lisp generic functions

Name: Anonymous 2011-03-05 6:52

Hi /prog/, this may disappoint you but this is a thread about programming... But it's written by a guy who wants help !
Basically, my question is:
With a generic function and its methods one can check the type/class of the variables that the genfun receives and the genfun therefore chooses the appropiate method, is there any possibility to make it check for the appropiate symbol?

If I can't make it check for the appropiate symbol, am I then forced to simply write one function with many cases?

Name: Anonymous 2011-03-05 7:05

http://www.lispworks.com/documentation/HyperSpec/Body/m_defmet.htm
I think what you're looking for is an [m]EQL[m] specializer.

(defgeneric genfun (arg))
(defmethod genfun ((arg (eql 'cat))) 'animal)
(defmethod genfun ((arg (eql 'dog))) 'animal)
(defmethod genfun ((arg (eql 'banana))) 'fruit)
(defmethod genfun (arg) 'unknown)

CL-USER> (genfun 'dog)
ANIMAL
CL-USER> (genfun 'whatever)
UNKNOWN
CL-USER> (genfun 'cat)
ANIMAL

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List