From: Mitch Whorlow
Subject: building a function from a list
Date: 
Message-ID: <Pine.LNX.3.96.1000817135614.31596A-100000@Godzilla.cs.nwu.edu>
I'd like to accomplish the following...

I have a rule database with the following format:

'(((citation-by-self) => 0)
  ((citation-by-co-author) (citation-after 1995) => 1)
  ((citation-by-co-author) => 2)
  ((citation-after 1995) => 6)
  ( => 4))

Where citation-by-self, etc. are simple functions like:

(defun citation-by-self (author citing-article)
  (eql (article-author citing-article) author))

I have defined all of the citation-* functions to take at least the author
and citing-article parameters to make things easier for me.  The number
values (0, 1, 2, 6, 4) represent the scores that should be awarded if the
rule matches. 

I'd like to transform the rule database mentioned above (which changes
frequently) into a closure, which for this particular example looks like:

'(lambda (author citing-article)
   (cond ((citation-by-self author citing-article) 0)
	 ((and (citation-by-co-author author citing-article)
	       (citation-after 1995 author citing-article)) 1)
	 ((citation-by-co-author author citing-article) 2)
	 ((citation-after 1995 author citing-article) 6)
	 (t 4)))

I have accomplished this task by using a whole bunch of list and append
function calls to build up a list of the above form.  Then I run a (coerce
... 'function) followed by a funcall. 

I do not like using the (coerce ... 'function), because it seems like a
cleaner way must exist.  I tried defining a macro, but I'm still a novice
to them, and was unsuccessful.  Is a macro the better solution?  If so,
help with the building the macro would be great.

Any help or insight is much appreciated.

Thanks,

Mitch
From: Frank A. Adrian
Subject: Re: building a function from a list
Date: 
Message-ID: <u_Dn5.682$aQ3.321918@news.uswest.net>
You could use (compile nil <lambda-expr>).  This will return a compiled
function.  One piece of advice, though - if you really are doing a buch of
cons'es and append's to generate your code, try backquoted expressions to at
least generate the chunks of code you're stringing together to generate the
source.  This could eliminate much of the tedium.

faa


"Mitch Whorlow" <········@Godzilla.cs.nwu.edu> wrote in message
··············································@Godzilla.cs.nwu.edu...
> I'd like to accomplish the following...
>
> I have a rule database with the following format:
>
> '(((citation-by-self) => 0)
>   ((citation-by-co-author) (citation-after 1995) => 1)
>   ((citation-by-co-author) => 2)
>   ((citation-after 1995) => 6)
>   ( => 4))
>
> Where citation-by-self, etc. are simple functions like:
>
> (defun citation-by-self (author citing-article)
>   (eql (article-author citing-article) author))
>
> I have defined all of the citation-* functions to take at least the author
> and citing-article parameters to make things easier for me.  The number
> values (0, 1, 2, 6, 4) represent the scores that should be awarded if the
> rule matches.
>
> I'd like to transform the rule database mentioned above (which changes
> frequently) into a closure, which for this particular example looks like:
>
> '(lambda (author citing-article)
>    (cond ((citation-by-self author citing-article) 0)
> ((and (citation-by-co-author author citing-article)
>        (citation-after 1995 author citing-article)) 1)
> ((citation-by-co-author author citing-article) 2)
> ((citation-after 1995 author citing-article) 6)
> (t 4)))
>
> I have accomplished this task by using a whole bunch of list and append
> function calls to build up a list of the above form.  Then I run a (coerce
> ... 'function) followed by a funcall.
>
> I do not like using the (coerce ... 'function), because it seems like a
> cleaner way must exist.  I tried defining a macro, but I'm still a novice
> to them, and was unsuccessful.  Is a macro the better solution?  If so,
> help with the building the macro would be great.
>
> Any help or insight is much appreciated.
>
> Thanks,
>
> Mitch
>