The return value of the Elisp function

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

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

But I have a mistake. I am not sure how to make the function return a value ... I read a function that returns the value of the result of the last expression, but in this case I will not do something like (warning about PHP invalid):

 // code if ($condition) { return false; } // more code... 

Maybe I'm missing a point, and functional programming does not allow me to use this approach?

+4
source share
1 answer

First , you need a list of arguments after tmr-active-timer-p ; defun syntax

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

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

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

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

then it will return nil if the file does not exist (since (when foo bar) matches (if foo (progn bar) nil) ).

Finally , parentheses are considered a bad style for formatting code in lisp.

PS. Emacs Lisp does not have return , but it does have Non-local outputs . I urge you to avoid them if you really do not know what you are doing.

+13
source

Source: https://habr.com/ru/post/1480758/


All Articles