Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elisp function return value

I'm having a (probably) dumb problem with Elisp. I want a function to return t or nil depending on a when condition. This is the code:

(defun tmr-active-timer-p
  "Returns t or nil depending of if there's an active timer"
  (progn
    (if (not (file-exists-p tmr-file))
        nil
      ; (... more code)
      )
    )
)

But I'm having an error. I'm not sure how to make a function return a value... I've read a function return the last expression result value but in this case, I wan't to make something like (PHP mess warning):

// code

if ($condition) {
  return false;
}

// more code...

Maybe I'm missing the point and functional programming doesn't allow this approach?

like image 648
hhaamm Avatar asked May 14 '13 16:05

hhaamm


1 Answers

First, you need an argument list after tmr-active-timer-p; the defun syntax is

(defun function-name (arg1 arg2 ...) code...)

Second, you do not need to wrap the body in progn.

Third, the return value is the last form evaluated. If your case you can just write

(defun tmr-active-timer-p ()
  "Returns t or nil depending of if there's an active timer."
  (when (file-exists-p tmr-file)
      ; (... more code)
    ))

then it will return nil if the file does not exist (because (when foo bar) is the same as (if foo (progn bar) nil)).

Finally, hanging parentheses are considered to be a poor code formatting style in lisp.

PS. Emacs Lisp does not have return, but it does have Nonlocal Exits. I urge you to avoid them unless you really know what you are doing.

like image 191
sds Avatar answered Nov 06 '22 00:11

sds