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

Pages: 1-

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

Name: Anonymous 2011-03-05 7:14

>>2
Oh yeah, that would be it! Thanks, really appreciate it.

Name: Anonymous 2011-03-05 7:15

Consider this: A pack of wild Niggers.
Savage, slavering Niggers nearing your white home. Trampling your white lawn. Raping your white daughter.
And you can't do shit since they're savages. The Nigger leader grabs your wife and fucks her with his shaman stick.
The primal Niggers finally dominate your household. They watch barbaric shows on TV and you are forced to be their slave.
Such is the downfall of White Man.

Name: Anonymous 2011-03-05 7:49

I suspect that the macro loop-map doesn't work because it accidently captures the variable map though I am unsure. I'm going to re-write these macros so they will use  gensym but is there something else I have missed?
The error I get when trying to run the macro loop-map is "map isn't of type sequence".

Source code:



(defmacro length-map (map)
  `(1- (length ,map)))

(defmacro loop-map (map &body body)
  `(loop for var1 from 0 to ,(length-map map) do
        (loop for var2 from 0 to ,(length-map (aref map 0)) do
             ,@body)))


A "map" as I call it looks something like this:

[code]

#( #(a a a)
   #(a a a))

Name: Anonymous 2011-03-05 8:12

>>6
Not entirely sure what you want to do, but if you just want to iterate over the array using loop, there are better ways to do it. Also, are you sure you want length-map to be a macro? My question to you is how do you want the generated code to look like? Are you sure you want to capture var1 and var2?

The reason your macro fails is because length-map is a macro and you'r trying to calculate (length 'map), not (length map) (compile-time vs run-time). It fails at compile-time when expanding the loop-map macro.

tl;dr: Decide what you need to run at compile-time and what you need to run at runtime. Get an idea of what the expanded code loops like if you want to write a macro which will generate it.

Name: Anonymous 2011-03-05 8:14

Name: Anonymous 2011-03-05 8:15

>>7
stack overflow

Name: >>6 2011-03-05 8:16

>>7
Erm, I meant >>5.

Name: Anonymous 2011-03-05 8:32

>>6
Yeah, I'm on my way solving this problem, I've kinda realised why it fails so miserably.

Please bear with me, first time I'm actually writing macros myself.

Name: Anonymous 2011-03-05 8:40

Okay, solved it on my own, thanks, if I get another problem I'll write another post here, though I'll sage so at least you won't get annoyed by another bump!

Name: Anonymous 2011-03-05 9:49

OP here. I decided to annoy you anyway.

Name: Anonymous 2011-03-06 6:26

>>12
B u t  t h a t ' s  n o t  t r u e  : ' (

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