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

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

Re: emacs lisp confusion using append


From: David Kastrup
Subject: Re: emacs lisp confusion using append
Date: 05 Jun 2003 08:53:13 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

Balaji Venkataraman <address@hidden> writes:

> I was trying to write some lisp code, but am completely boggled by this.
> 
> Here's what I originally had in my ~/.emacs:
> --WAS WORKING--
> (defun append-path (elisp-path)
>   "Appends ELISP-PATH path to the load-path.
> Emacs will look for additional .el files there"
>   (setq load-path (append load-path (list (expand-file-name elisp-path)))))
> 
> (append-path "~/usr/elisp")
> --WAS WORKING--

For some definition of "working".  This will not hesitate to add more
and more identical copies to the path each time you call it.

> Then I changed it to this:
> --NOT WORKING--
> (defun append-path (orig-path path)
>   "Appends PATH to ORIG-PATH. PATH is a string and ORIG-PATH is a variable.
> If ORIG-PATH is not defined, returns nil."
>   (if (boundp 'orig-path)
>       (setq orig-path (append orig-path (list (expand-file-name path))))
>     nil))
> 
> (append-path load-path "~/usr/elisp")
> --NOT WORKING--
> 
> Now "~/usr/elisp" is *missing* from the load-path. 

Well, but it _is_ in orig-path (before append-path exits).  Lisp is
call-by-value, not call-by-reference.

You need to use a macro to achieve call-by-reference.  Like

(defmacro append-path (orig-path path)
  `(and (boundp ',orig-path)
        (add-to-list ',orig-path ,path t)))

> I don't understand why the first one works and the second doesn't. I
> no lisp expert and I realise there are many deficiencies in my
> code. Is listp a better substitute for boundp?

listp is something completely different.  boundp checks whether a given
name has a value.  listp checks whether a given value is a list.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


reply via email to

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