0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Emacs Lisp 超初心者がコマンドを作ってみる 〜 if文のクセ 〜

Posted at

理解を進めるために、 Emacs Lisp の諸々について、他のプログラミング言語と見比べてみます。

if

EXCELのワークシート関数 if に似ています。 EXCEL のは if(condition, then, else) ですけども Emacs Lisp のは

( if condition then-form else-forms )

となっています。

前回より、再掲

(if (not untranslated-regions)
    (error "Nothing previous msgid."))

このように else-forms... を省略することもできます。

Emacs Lisp (Lisp?) の if のハマりどころとして、 then-form は「1個だけ」ですが、 else-form は0個以上というのがあります。

(if condition (A) (B))

これをC言語で書くと

#include <stdio.h>
void main(int argc, char *argv[]);

void main(int argc, char *argv[])
{
  if (argc > 1)
    printf("exit args\n");
  else {
    printf("no args\n");
  }
}

注意して欲しいのは then が 単文 ( { と } で囲まれてない) けど、 else は複文( { と } で囲まれている)という点です。

よって、

(if condition (A) (B) (C) (D) (E) (F))

はC言語でに書くと

#include <stdio.h>
void main(int argc, char *argv[]);

void main(int argc, char *argv[])
{
  if (argc > 1)
    printf("exit args\n");
  else {
    printf("no args\n");
    printf("C\n");
    printf("D\n");
    printf("E\n");
    printf("F\n");

  }
}

となります。 どうしても then に複数の文を入れたい時は progn を使います。

(if (conditon) (progn (A) (B) (C)) (D) (E) (F))

なので、 C言語で { } を追加するみたいに if-else 文を

(if conditon
    (
     (A) (B) (C)
    )
    (D) (E) (F)

とやると Invalid-function エラーで怒られます。

今回はここまで

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?