[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp - anonymous function in an association list?
From: |
apatheticagnostic |
Subject: |
Re: elisp - anonymous function in an association list? |
Date: |
Thu, 29 Nov 2007 07:52:00 -0800 (PST) |
User-agent: |
G2/1.0 |
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.
Re: elisp - anonymous function in an association list?, Tassilo Horn, 2007/11/29