2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Emacs Lisp では if のelse部に複数の式が書けます

Last updated at Posted at 2022-11-25

Emacs Lisp 以外の Lisp では、if のelse部に複数の式が書けません(progn を用いる必要がある)が、Emacs Lisp ではelse部に複数の式が書けます(暗黙のprognが適用される)。

Common Lisp
#! /usr/bin/env -S sbcl --script
(defun xprint (x) (print x))
(defparameter x 1)
(if (= x 0)
  (xprint 'zero)
  (xprint 'not-zero)
  ;(xprint 'not-zero2)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/lisp/if-else $ cd "d:/Users/javac/Documents/.repo/env/lisp/if-else/" && time "./if-else-1.lsp"
;;; 
;;; NOT-ZERO 0.280 secs
;;; ~/lisp/if-else $
Common Lisp(defun xprint (x) (print x))
#! /usr/bin/env -S sbcl --script
(defun xprint (x) (print x))
(defparameter x 1)
(if (= x 0)
  (xprint 'zero)
  (xprint 'not-zero)
  (xprint 'not-zero2)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/lisp/if-else $ cd "d:/Users/javac/Documents/.repo/env/lisp/if-else/" && time "./if-else-2.lsp"
;;; Unhandled SB-KERNEL::ARG-COUNT-ERROR in thread #<SB-THREAD:THREAD "main thread" RUNNING
;;;                                                   {1001DC0003}>:
;;;   Error while parsing arguments to DESTRUCTURING-BIND:
;;;     too many elements in
;;;       ((= X 0) (XPRINT 'ZERO) (XPRINT 'NOT-ZERO) (XPRINT 'NOT-ZERO2))
;;;     to satisfy lambda list
;;;       (TEST THEN &OPTIONAL ELSE):
;;;     between 2 and 3 expected, but got 4
;;; 
;;; unhandled condition in --disable-debugger mode, quitting
;;; 0.217 secs
;;; ~/lisp/if-else $
Emacs Lisp
#! /usr/bin/env -S emacs -batch -l
(load "~/xprint.el")
(setq x 1)
(if (= x 0)
  (xprint 'zero)
  (xprint 'not-zero)
  (xprint 'not-zero2)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/lisp/if-else $ cd "d:/Users/javac/Documents/.repo/env/lisp/if-else/" && time "./if-else-3.el"
;;; Loading d:/Users/javac/Documents/.repo/env/xprint.el (source)...
;;; not-zero
;;; not-zero2
;;; 0.317 secs
;;; ~/lisp/if-else $

xprint.el については、『Emacs Lisp でプリントデバッグをする (5) - Qiita』をご覧ください。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?