unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* elisp - anonymous function in an association list?
@ 2007-11-29 14:00 apatheticagnostic
  2007-11-29 14:50 ` Marc Tfardy
  2007-11-29 15:41 ` Tassilo Horn
  0 siblings, 2 replies; 9+ messages in thread
From: apatheticagnostic @ 2007-11-29 14:00 UTC (permalink / raw)
  To: help-gnu-emacs

Hey everyone - I'm rather new to the world of lisp in general, so I'm
probably missing something rather obvious here.

I've searched the documentation, tried googling around, searched on
emacswiki, and tried just about every permutation of the following
code that I could think of, and am having no success.

The goal is to have an anonymous function in an association list - I
have a feeling my not being able to get this to work is because of me
not having a clear understanding of how funcall works.

take, for example this code showing what I mean:

(defvar sample-alist '(("a" '(lambda ()
                                (message "We worked!")))
                       ("b" #'(lambda ()
                                (message "B worked too!")))))

(defun test-call (x)
  (funcall (cdr (assoc x sample-alist))))

(test-call "a")
(test-call "b")

Both calls fail, with an error message like so:

for "b": Debugger entered--Lisp error: (invalid-function ((function
(lambda nil (message "B worked too!")))))

for "a": Debugger entered--Lisp error: (invalid-function ((quote
(lambda nil (message "We worked!")))))

Oh, looks like it's a nested list, maybe calling the car works?

Nope.

So would someone be so kind as to explain what is going on here to
this poor, confused, wanna-be emacs-customizer?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 14:00 elisp - anonymous function in an association list? apatheticagnostic
@ 2007-11-29 14:50 ` Marc Tfardy
  2007-11-29 15:01   ` apatheticagnostic
  2007-11-29 15:41 ` Tassilo Horn
  1 sibling, 1 reply; 9+ messages in thread
From: Marc Tfardy @ 2007-11-29 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

apatheticagnostic schrieb:

> take, for example this code showing what I mean:
> 
> (defvar sample-alist '(("a" '(lambda ()
>                                 (message "We worked!")))
>                        ("b" #'(lambda ()
>                                 (message "B worked too!")))))
> 
> (defun test-call (x)
>   (funcall (cdr (assoc x sample-alist))))
> 
> (test-call "a")
> (test-call "b")
> 
> Both calls fail, with an error message like so:

This seems to work:

(defvar sample-alist '(("a" (lambda () (message "We worked!")))
                        ("b" (lambda () (message "B worked too!")))))

(defun test-call (x)
   (funcall (eval (car (cdr (assoc x sample-alist))))))


(test-call "a")
"We worked!"

(test-call "b")
"B worked too!"


regards

Marc

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 14:50 ` Marc Tfardy
@ 2007-11-29 15:01   ` apatheticagnostic
  2007-11-29 15:04     ` apatheticagnostic
  2007-11-29 15:32     ` David Kastrup
  0 siblings, 2 replies; 9+ messages in thread
From: apatheticagnostic @ 2007-11-29 15:01 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 29, 9:50 am, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
> apatheticagnostic schrieb:
>
> > take, for example this code showing what I mean:
>
> > (defvar sample-alist '(("a" '(lambda ()
> >                                 (message "We worked!")))
> >                        ("b" #'(lambda ()
> >                                 (message "B worked too!")))))
>
> > (defun test-call (x)
> >   (funcall (cdr (assoc x sample-alist))))
>
> > (test-call "a")
> > (test-call "b")
>
> > Both calls fail, with an error message like so:
>
> This seems to work:
>
> (defvar sample-alist '(("a" (lambda () (message "We worked!")))
>                         ("b" (lambda () (message "B worked too!")))))
>
> (defun test-call (x)
>    (funcall (eval (car (cdr (assoc x sample-alist))))))
>
> (test-call "a")
> "We worked!"
>
> (test-call "b")
> "B worked too!"
>
> regards
>
> Marc

So it does. Well, I feel dumb now. I assumed that I would need to
quote the lambda expressions in some way to prevent them from being
evaluated at definition.

Thanks a lot!

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 15:01   ` apatheticagnostic
@ 2007-11-29 15:04     ` apatheticagnostic
  2007-11-30  0:08       ` Johan Bockgård
  2007-11-29 15:32     ` David Kastrup
  1 sibling, 1 reply; 9+ messages in thread
From: apatheticagnostic @ 2007-11-29 15:04 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 29, 10:01 am, apatheticagnostic <apatheticagnos...@gmail.com>
wrote:
> On Nov 29, 9:50 am, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
>
>
>
> > apatheticagnostic schrieb:
>
> > > take, for example this code showing what I mean:
>
> > > (defvar sample-alist '(("a" '(lambda ()
> > >                                 (message "We worked!")))
> > >                        ("b" #'(lambda ()
> > >                                 (message "B worked too!")))))
>
> > > (defun test-call (x)
> > >   (funcall (cdr (assoc x sample-alist))))
>
> > > (test-call "a")
> > > (test-call "b")
>
> > > Both calls fail, with an error message like so:
>
> > This seems to work:
>
> > (defvar sample-alist '(("a" (lambda () (message "We worked!")))
> >                         ("b" (lambda () (message "B worked too!")))))
>
> > (defun test-call (x)
> >    (funcall (eval (car (cdr (assoc x sample-alist))))))
>
> > (test-call "a")
> > "We worked!"
>
> > (test-call "b")
> > "B worked too!"
>
> > regards
>
> > Marc
>
> So it does. Well, I feel dumb now. I assumed that I would need to
> quote the lambda expressions in some way to prevent them from being
> evaluated at definition.
>
> Thanks a lot!

Sorry for the double-post but I wanted to point out that it works with
just (funcall (car (cdr (assoc x sample-list)))) and doesn't seem to
need the eval.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 15:01   ` apatheticagnostic
  2007-11-29 15:04     ` apatheticagnostic
@ 2007-11-29 15:32     ` David Kastrup
  2007-11-29 15:52       ` apatheticagnostic
  2007-11-29 21:24       ` Barry Margolin
  1 sibling, 2 replies; 9+ messages in thread
From: David Kastrup @ 2007-11-29 15:32 UTC (permalink / raw)
  To: help-gnu-emacs

apatheticagnostic <apatheticagnostic@gmail.com> writes:

> On Nov 29, 9:50 am, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
>> apatheticagnostic schrieb:
>>
>> > take, for example this code showing what I mean:
>>
>> > (defvar sample-alist '(("a" '(lambda ()
>> >                                 (message "We worked!")))
>> >                        ("b" #'(lambda ()
>> >                                 (message "B worked too!")))))
>>
>> > (defun test-call (x)
>> >   (funcall (cdr (assoc x sample-alist))))
>>
>> > (test-call "a")
>> > (test-call "b")
>>
>> > Both calls fail, with an error message like so:
>>
>> This seems to work:
>>
>> (defvar sample-alist '(("a" (lambda () (message "We worked!")))
>>                         ("b" (lambda () (message "B worked too!")))))

[fixed to omit the utterly pointless eval:]

>> (defun test-call (x)
>>    (funcall (car (cdr (assoc x sample-alist)))))

> So it does. Well, I feel dumb now. I assumed that I would need to
> quote the lambda expressions in some way to prevent them from being
> evaluated at definition.

lambda is self-quoting.  It is preferable not to quote it nevertheless,
because then the byte compiler can compile and optimize it.

However, that was not the problem.  The problem is that (cdr '(a b)) is
not b, but rather something equal to '(b).  So you either need to write

(defvar sample-alist '(("a" . (lambda () (message "We worked!")))
                       ("b" . (lambda () (message "B worked too!")))))

_or_ otherwise need the (cadr (assoc... rather than (cdr
(assoc... construct.
-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 14:00 elisp - anonymous function in an association list? apatheticagnostic
  2007-11-29 14:50 ` Marc Tfardy
@ 2007-11-29 15:41 ` Tassilo Horn
  1 sibling, 0 replies; 9+ messages in thread
From: Tassilo Horn @ 2007-11-29 15:41 UTC (permalink / raw)
  To: help-gnu-emacs

apatheticagnostic <apatheticagnostic@gmail.com> writes:

Hi,

> Both calls fail, with an error message like so:
>
> for "b": Debugger entered--Lisp error: (invalid-function ((function
> (lambda nil (message "B worked too!")))))
>
> for "a": Debugger entered--Lisp error: (invalid-function ((quote
> (lambda nil (message "We worked!")))))

This works:

--8<---------------cut here---------------start------------->8---
(defvar sample-alist '(("a" (lambda ()
                              (message "We worked!")))
                       ("b" (lambda ()
                              (message "B worked too!")))))

(defun test-call (x)
  (funcall (cadr (assoc x sample-alist))))

(test-call "a")
(test-call "b")
--8<---------------cut here---------------end--------------->8---

First, you don't need to quote the lambdas inside the list, which is
already quoted.  Second in `test-call' you don't want the cdr but the
car of the cdr (= cadr) from the assoc.

Bye,
Tassilo

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 15:32     ` David Kastrup
@ 2007-11-29 15:52       ` apatheticagnostic
  2007-11-29 21:24       ` Barry Margolin
  1 sibling, 0 replies; 9+ messages in thread
From: apatheticagnostic @ 2007-11-29 15:52 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 29, 10:32 am, David Kastrup <d...@gnu.org> wrote:
> apatheticagnostic <apatheticagnos...@gmail.com> writes:
> > On Nov 29, 9:50 am, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
> >> apatheticagnostic schrieb:
>
> >> > take, for example this code showing what I mean:
>
> >> > (defvar sample-alist '(("a" '(lambda ()
> >> >                                 (message "We worked!")))
> >> >                        ("b" #'(lambda ()
> >> >                                 (message "B worked too!")))))
>
> >> > (defun test-call (x)
> >> >   (funcall (cdr (assoc x sample-alist))))
>
> >> > (test-call "a")
> >> > (test-call "b")
>
> >> > Both calls fail, with an error message like so:
>
> >> This seems to work:
>
> >> (defvar sample-alist '(("a" (lambda () (message "We worked!")))
> >>                         ("b" (lambda () (message "B worked too!")))))
>
> [fixed to omit the utterly pointless eval:]
>
> >> (defun test-call (x)
> >>    (funcall (car (cdr (assoc x sample-alist)))))
> > So it does. Well, I feel dumb now. I assumed that I would need to
> > quote the lambda expressions in some way to prevent them from being
> > evaluated at definition.
>
> lambda is self-quoting.  It is preferable not to quote it nevertheless,
> because then the byte compiler can compile and optimize it.

Ahh that's nice to know.

> However, that was not the problem.  The problem is that (cdr '(a b)) is
> not b, but rather something equal to '(b).

Oh, I think I get it - because lists are normally a strung-along . . .
list . . . of thing and pointer to next thing without the . syntax,
right? Because (cdr '(a b)) is '(b . ()) -> '(b) whereas (cdr '(a .
b)) is just b.(there's no implicit cdr of b, because we're assigning
the car and cdr of '(a . b) directly, skipping over the auto-list-
construction stuff.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 15:32     ` David Kastrup
  2007-11-29 15:52       ` apatheticagnostic
@ 2007-11-29 21:24       ` Barry Margolin
  1 sibling, 0 replies; 9+ messages in thread
From: Barry Margolin @ 2007-11-29 21:24 UTC (permalink / raw)
  To: help-gnu-emacs

In article <85k5o13w4q.fsf@lola.goethe.zz>, David Kastrup <dak@gnu.org> 
wrote:

> apatheticagnostic <apatheticagnostic@gmail.com> writes:
> 
> > On Nov 29, 9:50 am, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
> >> apatheticagnostic schrieb:
> >>
> >> > take, for example this code showing what I mean:
> >>
> >> > (defvar sample-alist '(("a" '(lambda ()
> >> >                                 (message "We worked!")))
> >> >                        ("b" #'(lambda ()
> >> >                                 (message "B worked too!")))))
> >>
> >> > (defun test-call (x)
> >> >   (funcall (cdr (assoc x sample-alist))))
> >>
> >> > (test-call "a")
> >> > (test-call "b")
> >>
> >> > Both calls fail, with an error message like so:
> >>
> >> This seems to work:
> >>
> >> (defvar sample-alist '(("a" (lambda () (message "We worked!")))
> >>                         ("b" (lambda () (message "B worked too!")))))
> 
> [fixed to omit the utterly pointless eval:]
> 
> >> (defun test-call (x)
> >>    (funcall (car (cdr (assoc x sample-alist)))))
> 
> > So it does. Well, I feel dumb now. I assumed that I would need to
> > quote the lambda expressions in some way to prevent them from being
> > evaluated at definition.
> 
> lambda is self-quoting.  It is preferable not to quote it nevertheless,
> because then the byte compiler can compile and optimize it.

The self-quoting of lambda is not the issue here.  He already quoted the 
list -- the problem was that he embedded another quote within the list.  
This simply quoted the quote.
-- 
Barry Margolin
Arlington, MA

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: elisp - anonymous function in an association list?
  2007-11-29 15:04     ` apatheticagnostic
@ 2007-11-30  0:08       ` Johan Bockgård
  0 siblings, 0 replies; 9+ messages in thread
From: Johan Bockgård @ 2007-11-30  0:08 UTC (permalink / raw)
  To: help-gnu-emacs

apatheticagnostic <apatheticagnostic@gmail.com> writes:

> Sorry for the double-post but I wanted to point out that it works with
> just (funcall (car (cdr (assoc x sample-list)))) and doesn't seem to
> need the eval.

If you use this form there is no need for `car'

     (setq alist '((a . 1) (b . 2)))
     (cdr (assoc 'b alist))
        =>  2

(Also, (car (cdr X)) == (cadr X).)

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-11-30  0:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-29 14:00 elisp - anonymous function in an association list? apatheticagnostic
2007-11-29 14:50 ` Marc Tfardy
2007-11-29 15:01   ` apatheticagnostic
2007-11-29 15:04     ` apatheticagnostic
2007-11-30  0:08       ` Johan Bockgård
2007-11-29 15:32     ` David Kastrup
2007-11-29 15:52       ` apatheticagnostic
2007-11-29 21:24       ` Barry Margolin
2007-11-29 15:41 ` Tassilo Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).