Table of Contents

  1. If statement in elisp
    1. To remember
      1. Consider the following examples:

TODO If statement in elisp

To remember

According to emacs manual if statement has the following form:

(if nil
    (print 'true)
  'very-false)
⇒ very-false

It is important to remember that this statement has the form: if condition then-form else-forms ... i.e. if condition is fulfilled then-form is evaluated, otherwise the rest of the lines inside external parenthesis.

This means that:

condition is fulfilled they need to be embraced by parenthesis and add a special keyword progn (see Sequencing in emacs manual)!!!

condition is not fulfilled they shouldn’t be embraced by parenthesis, because otherwise they are treated as a function! According to emacs manual: ‘‘The else part of if is an example of an implicit progn. See Sequencing.’’

Consider the following examples:

  1. Working as intended

    (if t
        (progn
         (print "TRUE: The condition ...")
         (print "... is fulfilled")
        )
         (print "FALSE: The condition ...")
         (print "... is not fulfilled")      
    )
    
    (if nil
        (progn
         (print "TRUE: The condition ...")
         (print "... is fulfilled")
        )
         (print "FALSE: The condition ...")
         (print "... is not fulfilled")      
    )
    
  2. Unexpected behaviour

    (if nil
         (print "TRUE: The condition ...")
         (print "... is fulfilled")
         (print "FALSE: The condition ...")
         (print "... is not fulfilled")      
    )
    
    (if t
         (print "TRUE: The condition ...")
         (print "... is fulfilled")
         (print "FALSE: The condition ...")
         (print "... is not fulfilled")      
    )