Notes on Programming in Emacs Lisp (2)

2018/04/08

3. Function

(defun multiply-by-seven (number)
  "Multiply NUMBER by seven."
  (* 7 number))

(multiply-by-seven 3)
21

;; make function interactive
(defun multiply-by-seven (number)
  "Multiply NUMBER by seven."
  (interactive "p") ; p - pass the prefix argument to the function
  (message "The result is %d" (* 7 number)))
(let ((variable value)
      (variable value)
      ...)
  body...)
(let ((zebra "stripes")
      (tiger "fierce"))
  (message "One kind of animal has %s and another is %s."
           zebra tiger))
(let ((birch 3)
      pine
      fir
      (oak 'some))
  (message
   "Here are %d variables with %s, %s, and %s value."
   birch pine fir oak))
"Here are 3 variables with nil, nil, and some value."
(setq my-name "anchu")
(if (equal my-name "anchu")
    (message "My name is %s" my-name))
"My name is anchu"
(defun type-of-animal (characteristic)  ; Second version.
  "Print message in echo area depending on CHARACTERISTIC.
     If the CHARACTERISTIC is the string \"fierce\",
     then warn of a tiger; else say it is not fierce."
  (if (equal characteristic "fierce")
      (message "It is a tiger!")
    (message "It is not fierce!")))

(type-of-animal "fierce")
"It is a tiger!"

(type-of-animal "striped")
"It is not fierce!"
(save-excursion
  first-expression-in-body
  second-expression-in-body
  third-expression-in-body
  ...
  last-expression-in-body)

4. Basic operation

;; Arithmetic
(+ 20 30)
50

(- 100 80)
20

(+ 1 2 3 4 5)
15

(* 1 2 3 4 5)
120

(/ 100 20)
5

(> 10 1)
t

(< 2 8)
t

(< 8 2)
nil

(= 2 4)
nil

(equal 2 2)
t

(exp -1)
0.36787944117144233

(log 10)
2.302585092994046

(sin pi)
1.2246467991473532e-16

(tan 90)
-1.995200412208242
;; Comparision
(equal (list 1 2 3 4) (list 1 2 3 4))
t

(eq 'x 'x)
t

(string= "a" "a")
t

(string= "a" "A")
nil

(string-equal "z" "z")
t

(string< "a" "b")
t

(string-lessp "a" "b")
t

(equal '(1 2) (list 1 2))
t

(eq '(1 2) (list 1 2))
nil
(type-of 1)
integer

(type-of "12")
string

(type-of '(1 2 3))
cons

(type-of '(a . "e"))
cons

(type-of (current-buffer))
buffer

(type-of 'current-buffer)
symbol