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

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

Re: elisp: returning a local variable


From: W. Greenhouse
Subject: Re: elisp: returning a local variable
Date: Tue, 12 Mar 2013 16:38:33 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

Glen,

Glen Stark <mail@glenstark.net> writes:

> Hi everyone.
>
> I'm doing some stuff in elisp, largely in an effort to become competent 
> in it.  I would like to obtain a list of subdirectories in the current 
> working directory.  I couldn't find a method that does this, so I tried 
> writing my own.  I got this far:
>
>
> (defun get-all-subdirectories ()
>   "Returns a list of directories in the current working folder"
>   (interactive)
>   (let ((src-list (file-expand-wildcards "*"))
>         (retval ()))
>     (dolist (fname src-list)
>       (when (file-directory-p fname) (push fname retval))
>       )
>     )
>   )   
>
> Which does indeed assemble a list of subdirectories into retval, but I 
> can't figure out how to return it as the return value of the function 
> (yes, I could setq to global variable, but I prefer not to do this).
>
> Can anyone help me out here?  
>       1)  how do I accomplish this?  
>       2)  Is there an existing method that does this?
>
> Thanks for your help.
>
> Glen

To return the value of a let-bound variable as the return value of a
function, simply make that variable, without quotations or parens, the
last element of the last expression in the function.  This works because
a variable evaluates to its own value.  For example:

(defun get-all-subdirectories ()
  "Returns a list of directories in the current working folder"
  (interactive)
  (let ((src-list (file-expand-wildcards "*"))
        (retval ()))
    (dolist (fname src-list)
      (when (file-directory-p fname) (push fname retval))
      )
    retval)
  )     


-- 
Regards,
WGG




reply via email to

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