help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using setq to obtain a symbol from a list, so that I can assign a fu


From: Barry Margolin
Subject: Re: Using setq to obtain a symbol from a list, so that I can assign a function to it
Date: Wed, 23 Apr 2008 22:50:26 -0400
User-agent: MT-NewsWatcher/3.5.3b2 (Intel Mac OS X)

In article 
<address@hidden>,
 address@hidden wrote:

> On Apr 22, 5:54 am, Barry Margolin <address@hidden> wrote:
> > In article
> > <address@hidden>,
> >
> >  address@hidden wrote:
> > > Oops... I got the subject wrong. Instead of
> >
> > > " Using setq to obtain a symbol from a list, so that I can assign a
> > > function to it", it should read, "Using setq to assign value to the
> > > result of a function". Sorry about that.
> >
> > Use set instead of setq.
> >
> > --
> > Barry Margolin, address@hidden
> > Arlington, MA
> > *** PLEASE post questions in newsgroups, not directly to me ***
> > *** PLEASE don't copy me on replies, I'll read them in the group ***
> 
> Thanks for the tip. Now, it does not throw an error. But I still don't
> get what I want. Here is what I did:
> 
> (setq grammar '((sentence ::= subject predicate)
>               (subject ::= article noun)
>               (predicate ::= verb)))
> --> This of course worked. I could see the individual elements, their
> car's and cdr's etc.
> 
> Then I defined the "leaf" level things:
> 
> (defun article()
>   (insert "article"))
> 
> (defun noun()
>   (insert "noun"))
> 
> (defun verb()
>   (insert "verb"))
> 
> Then I tried this:
> 
> (dolist (x grammar)
>   (set (car x) (dolist (y (cdr (cdr x))) (funcall y)))
> 
> It threw an error saying that it did not know what subject was. Of
> course, the way the grammar was described in the list, it did not know
> that. So I tried reversing the grammar, thinking that if it went the
> other way, it would understand subject before it came to sentence. So,
> I tried this.
> 
> (dolist (x (reverse grammar))
>   (set (car x) (dolist (y (cdr (cdr x))) (funcall y))))
> 
> It still gave the same error! It seems that when it does a set
> operation, it forgets it in the next iteration of the loop.

There are several things wrong here.

First, you're effectively doing:

(setq subject ...)

and then

(funcall 'subject)

But funcall expects subject to be a function name, not a variable name.  
What you probably want to do is:

(setf (symbol-function (car x)) ...)

The other problem is that you're not even assigning a function.  You're 
setting subject to the return value of dolist, which is just nil.  I'm 
not really sure how to recode what you're doing to get this right.

You're obviously trying to write a parser-generator in Lisp.  This has 
been done many times, I suggest you check Lisp code repositories, and 
see how they've done it.

-- 
Barry Margolin, address@hidden
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]