Elisp: how can I express else-if

In elisp, the logic of an if statement allows only the case of if and else.

(if (< 3 5) ; if case (foo) ; else case (bar)) 

but what if I want to do else-if? Do I need to introduce a new if statement inside the else case? It is just a little dirty.

+6
source share
1 answer

Attachment if

Because the parts (if test-expression then-expression else-expression) an else if would have to insert the new if as else-expression :

 (if test-expression1 then-expression1 (if test-expression2 then-expression2 else-expression2)) 

Using cond

In other languages, else if usually at the same level. For lisps for this we have cond . Here is the same with cond :

 (cond (test-expression1 then-expression1) (test-expression2 then-expression2) (t else-expression2)) 

Note that the expression may be just that. Any expression, so often, is similar to (some-test-p some-variable) , and other expressions are usually the same. Very rarely, these are just single characters to evaluate, but it can be for very simple conventions.

+11
source

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


All Articles