all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Barry Margolin <barmar@alum.mit.edu>
To: help-gnu-emacs@gnu.org
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	[thread overview]
Message-ID: <barmar-D6CEBF.22502623042008@newsgroups.comcast.net> (raw)
In-Reply-To: 530940ca-d019-42d4-ba15-c8674a8eb858@59g2000hsb.googlegroups.com

In article 
<530940ca-d019-42d4-ba15-c8674a8eb858@59g2000hsb.googlegroups.com>,
 srinik001@hotmail.com wrote:

> On Apr 22, 5:54 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> > In article
> > <a4bb6deb-6337-43bd-95e4-64a29770c...@y18g2000pre.googlegroups.com>,
> >
> >  srinik...@hotmail.com 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, bar...@alum.mit.edu
> > 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, barmar@alum.mit.edu
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 ***


  parent reply	other threads:[~2008-04-24  2:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-22  2:28 Using setq to obtain a symbol from a list, so that I can assign a function to it srinik001
2008-04-22  5:19 ` srinik001
2008-04-22 12:54   ` Barry Margolin
2008-04-23  4:40     ` srinik001
2008-04-23 22:14       ` Pascal Bourguignon
2008-04-24  2:50       ` Barry Margolin [this message]
2008-04-28  1:03         ` srinik001
2008-04-23  5:10   ` Kevin Rodgers
     [not found]   ` <mailman.10729.1208927476.18990.help-gnu-emacs@gnu.org>
2008-05-17 22:14     ` David Combs
2008-05-20  2:42       ` Kevin Rodgers
2008-04-22 15:22 ` Ted Zlatanov
2008-04-22 18:36   ` Pascal Bourguignon
2008-05-17 22:20     ` David Combs
2008-04-22 19:59 ` Pascal Bourguignon
2008-04-23  3:11   ` Timothy Hobbs
     [not found]   ` <mailman.10727.1208920326.18990.help-gnu-emacs@gnu.org>
2008-04-23  4:06     ` Barry Margolin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=barmar-D6CEBF.22502623042008@newsgroups.comcast.net \
    --to=barmar@alum.mit.edu \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.