-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.el
81 lines (68 loc) · 2.06 KB
/
11.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
;; Write a function similar to ‘triangle’ in which each row has a
;; value which is the square of the row number. Use a ‘while’ loop.
(defun triangle(rows)
" A triangle function with each row as
square of row number"
(let ((count 1)
(total 0))
(while (< count (1+ rows))
(setq total (+ total (* count count)))
(setq count (1+ count)))
total))
(triangle 5)
;; Write a function similar to ‘triangle’ that multiplies instead of
;; adds the values.
(defun triangle-mul(rows)
"Same as triangle but multiplies instead of addition"
(let ((count 1)
(total 1))
(while (< count (1+ rows))
(setq total (* total (* count count)))
(setq count (1+ count)))
total))
(triangle-mul 5)
;; Rewrite these two functions recursively. Rewrite these functions
;; using ‘cond’.
(defun triangle-recursive (rows)
"Same as 'triangle' but using 'recusion' and
'cond'"
(cond ((= rows 1)
1)
(t
(+ (* rows rows) (triangle-recursive (1- rows)))))
)
(triangle-recursive 5)
(defun triangle-mul-recursive (rows)
"Same as 'triangle' but using 'recusion' and
'cond'"
(cond ((= rows 1)
1)
(t
(* (* rows rows) (triangle-mul-recursive (1- rows)))))
)
(triangle-mul-recursive 5)
;; Write a function for Texinfo mode that creates an index entry at
;; the beginning of a paragraph for every ‘@dfn’ within the paragraph.
;; (In a Texinfo file, ‘@dfn’ marks a definition. This book is
;; written in Texinfo.)
(defun create-index-entry ()
"In texinfo all the function definitions were written that starts with
'@dfn{...}'. This function extracts all @dfns and creates index entry for each one"
(interactive)
(save-excursion
(mark-paragraph)
(save-restriction
(let (dfns)
(while (search-forward "@dfn" nil t)
(when (search-forward "{" nil t)
(let ((start (point)))
(when (search-forward "}" nil t)
(forward-char -1)
(let ((end (point)))
(setq dfns
(cons
(buffer-substring start end)
dfns)))))))
(forward-paragraph -1)
(dolist (dfn dfns)
(insert (concat "@cindex " dfn "\n")))))))