* emacs lisp confusion using append
@ 2003-06-05 2:39 Balaji Venkataraman
2003-06-05 6:53 ` David Kastrup
2003-06-05 14:20 ` Stefan Monnier
0 siblings, 2 replies; 3+ messages in thread
From: Balaji Venkataraman @ 2003-06-05 2:39 UTC (permalink / raw)
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--
This puts "~/usr/elisp" as the last elem of load-path.
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.
I did reach for the lisp reference manual and found this footnote:
(1) There is no strictly equivalent way to add an element to the end
of a list. You can use `(append LISTNAME (list NEWELT))', which
creates a whole new list by copying LISTNAME and adding NEWELT to its
end. Or you can use `(nconc LISTNAME (list NEWELT))', which modifies
LISTNAME by following all the CDRs and then replacing the terminating
`nil'. Compare this to adding an element to the beginning of a list
with `cons', which neither copies nor modifies the list.
Also the manual says: "but the last one should usually be a list. All
arguments except the last one are copied, so none of the arguments is
altered."
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? Help appreciated.
Thanks!
--
Remember 2 + 2 = 5, for large values of 2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: emacs lisp confusion using append
2003-06-05 2:39 emacs lisp confusion using append Balaji Venkataraman
@ 2003-06-05 6:53 ` David Kastrup
2003-06-05 14:20 ` Stefan Monnier
1 sibling, 0 replies; 3+ messages in thread
From: David Kastrup @ 2003-06-05 6:53 UTC (permalink / raw)
Balaji Venkataraman <bvenkata+nospam@sm.intel.com> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: emacs lisp confusion using append
2003-06-05 2:39 emacs lisp confusion using append Balaji Venkataraman
2003-06-05 6:53 ` David Kastrup
@ 2003-06-05 14:20 ` Stefan Monnier
1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2003-06-05 14:20 UTC (permalink / raw)
> (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))
You're confusing variable names and variable values.
Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-06-05 14:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-06-05 2:39 emacs lisp confusion using append Balaji Venkataraman
2003-06-05 6:53 ` David Kastrup
2003-06-05 14:20 ` Stefan Monnier
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).