【问题标题】:How can you write multiple statements in elisp 'if' statement?如何在 elisp 'if' 语句中编写多个语句?
【发布时间】:2010-10-29 01:56:58
【问题描述】:

在 elisp 中,有一个“如果”的情况,我想执行许多不同的事情:

(if condition
    (do-something)
    (do-something-else)
    ...)

但是,(do-something-else) 仅在 else-case 中执行。如何指定要执行的指令块?例如:

(if condition
    (begin
        (do-something)
        (do-something-else)
        ...))

【问题讨论】:

    标签: syntax elisp


    【解决方案1】:

    使用progn:

    (if condition
        (progn
            (do-something)
            (do-something-else)))
    

    【讨论】:

      【解决方案2】:

      如果不需要else,则使用起来可能更具可读性:

      (when condition
          (do-something)
          (do-something-else))
      

      反之亦然

      (unless (not condition)
          (do-something)
          (do-something-else))
      

      查看Emacs Lisp manual for conditionals

      【讨论】:

      • FWIW,我通常遵循 Common Lisp The Language 中建议的约定,即在返回值不重要时使用whenunless(即使用它们)仅用于副作用)。当返回值很重要时,我一般使用andor。我一般在有多个分支时使用ifcond(不管返回值是否重要)。
      猜你喜欢
      • 1970-01-01
      • 2016-06-28
      • 2016-08-30
      • 2021-10-13
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      相关资源
      最近更新 更多