iT邦幫忙

DAY 10
1

EMACS 新思維系列 第 10

[EMACS新思維 第十三天] Elisp 入門(三): elisp 語法入門 (二)

本基本介紹翻譯自 Emacs 高手 Xah Lee 的介紹文章,予以中文化!
七:If、Then、Else

條件語句的語法是: (if ‹test› ‹body›)。若欲包含 else 的部分,則是 (if ‹test› ‹true body› ‹false body›)

(if (< 3 2) (message "yes") )
(if (< 3 2) (message "yes") (message "no") )

(if nil (message "yes") (message "no") )  ; prints no

詳情可見:(info "(elisp) Control Structures")

若不需要 else,也可改用 when 這個函數。他的結構是: (when ‹test› ‹expr1› ‹expr2› …),等同於 (if ‹test› (progn ‹expr1› ‹expr2› …))

八:語句塊:

如何組織一連串的程式句?可以使用 progn

(progn (message "a") (message "b"))
;; 等同於
(message "a") (message "b")

(progn …) 與 C 語言中的 {…} 類似。共用是可將多個句子當成一個句子來處理與對待。

(if something
    (progn ; true
    …
    )
    (progn ; else
    …
    )
)

詳情可見:(info "(elisp) Sequencing")

九:迭代(Iteration)

底下是一段使用 while 函數的 loop 結構,他的語法是:(while ‹test› ‹body›),‹body› 是一個或多個 elisp 語句。

(setq x 0)

(while (< x 4)
  (print (format "yay %d" x))
  (setq x (1+ x)))

詳情可見:(info "(elisp) Iteration")
底下的程式碼執行 unicode char 32 轉換到 126 的工作。首先設定局部變數,然後啓動 while loop,將對應的 unicode char 插入,然後增加計數單位 1

(let ((x 32))
  (while (< x 127)
    (ucs-insert x)
    (setq x (+ x 1))))

注意:在 elisp 中,沒 for 這樣的結構

Emacs Lisp: Throw & Catch, Exit a Loop.


上一篇
[EMACS新思維 第十二天] Elisp 入門(二): elisp 語法入門 (一)
下一篇
[EMACS新思維 第十四天] Elisp 入門(四): defun,函數的定義
系列文
EMACS 新思維27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言