Skip to content

Latest commit

 

History

History
383 lines (298 loc) · 7.28 KB

eintr.org

File metadata and controls

383 lines (298 loc) · 7.28 KB

“An Introduction to Programming in Emacs Lisp” Exercises

(info “(eintr) Error Message Exercises”)

foo
(foo)
(setq counter 0)

(setq counter (+ counter 2))
(message "Hello %s!" user-full-name)

(info “(eintr) Evaluation Exercise”)

(goto-char (/ (buffer-size) 2))
(buffer-name)
(buffer-file-name)
(buffer-size)
(point)

(info “(eintr) defun Exercises”)

(defun double (number)
  "Double NUMBER."
  (* number 2))
(double 3)
(defun double (number)
  "Double NUMBER."
  (interactive "p")
  (message "The result is %d" (* number 2)))
(defun foo (number)
  (interactive "p")
  (if (> number fill-column)
      (message "%d is larger than fill-column" number)))

(info “(eintr) Buffer Exercises”)

(defun simplified-end-of-buffer ()
  "Move point to the end of the buffer, leave mark at previous position."
  (interactive)
  (push-mark)
  (goto-char (point-max)))
(defun test-if-a-buffer-exists (buffer)
  (interactive "BBuffer: ")
  (if (get-buffer buffer)
      (message "Buffer %s exists" buffer)))
(test-if-a-buffer-exists "*Help*")
(xref-find-definitions "copy-to-buffer")

(info “(eintr) optional Exercise”)

(defun foo (&optional arg)
  (interactive "p")
  (unless arg
    (setq arg 50))
  (if (>= arg fill-column)
      (message "%s is equal or larger than fill-column" arg)
    (message "%s is smaller than fill-column" arg)))

(info “(eintr) narrow Exercise”)

(defun narrowing-exercise ()
  "Display the first 60 characters of the current buffer."
  (save-restriction
    (widen)
    (message "%s" (buffer-substring (point-min) (+ (point-min) 60)))))

(narrowing-exercise)

(info “(eintr) cons Exercise”)

(cons 'a '(b c d))
(setq birds ())
(setq birds (cons 'd birds))
(setq birds (cons 'c birds))
(setq birds (cons 'b birds))
(setq birds (cons 'a birds))
birds
(let ((birds '(a b c d)))
  (setcar birds 'A)
  (setcdr birds '(B C D))
  birds)

(info “(eintr) search Exercises”)

(defun test-search (pattern)
  (interactive "sSearch for: ")
  (search-forward pattern)
  (message "Found!"))
(defun print-third-kill-ring ()
  (interactive)
  (if (> (length kill-ring) 2)
      (message "%s" (nth 2 kill-ring))
    (message "kill-ring doesn't have at least three elements")))

(info “(eintr) List Exercise”)

(setq flowers '(violet buttercup))
(setq more-flowers (cons 'bar (cons 'foo flowers)))
(setcar flowers 'fish)
more-flowers

(info “(eintr) yank nthcdr Exercises”)

(let ((numbers '(1 2 3 4)))
  (list (car numbers)
        (car (nthcdr 1 numbers))
        (car (nthcdr 2 numbers))
        (car (nthcdr 3 numbers))))

(info “(eintr) Looping exercise”)

(defun triangle-square (number-of-rows)
  (let ((total 0)
        (row-number 1))
    (while (<= row-number number-of-rows)
      (setq total (+ total (* row-number row-number)))
      (setq row-number (1+ row-number)))
    total))

(triangle-square 4)
(defun triangle-multiple (number-of-rows)
  (let ((total 1)
        (row-number 1))
    (while (<= row-number number-of-rows)
      (setq total (* total row-number))
      (cl-incf row-number))
    total))

(= (triangle-multiple 4)
   (* 1 2 3 4))
(defun triangle-square (number-of-rows)
  (if (= number-of-rows 0)
      0
    (+ (* number-of-rows number-of-rows)
       (triangle-square (1- number-of-rows)))))

(triangle-square 4)
(defun triangle-multiple (n)
  (if (= n 0) 1
    (* n (triangle-multiple (1- n)))))

(triangle-multiple 4)

(info “(eintr) re-search Exercises”)

(defun blank-lines ()
  (interactive)
  (re-search-forward "^\\(?:[ \t]*\n\\)\\{2,\\}"))
(defun the-the ()
  (interactive)
  (if (re-search-forward
       "\\b\\([^@ \n\t]+\\)[ \n\t]+\\1\\b" nil 'move)
      (message "Found duplicated word.")
    (message "End of buffer")))
one two two three four five
five six seven

(info “(eintr) Counting Exercise”)

(defun count-puncuation (beg end)
  "Print number of punctuation marks in the region."
  (interactive "r")
  (message "Counting punctuation marks in the region...")
  (save-excursion
    (goto-char beg)
    (let ((count 0))
      (while (and (< (point) end)
                  (re-search-forward "[.,;:!?]" end t))
        (setq count (1+ count)))
      (cond ((zerop count)
             (message
              "The region does NOT have any punctuation marks."))
            ((= 1 count)
             (message
              "The region has 1 punctuation mark."))
            (t
             (message
              "The region has %d punctuation marks." count))))))
(defun recursive-count-punctuation (region-end)
  "Number of punctuation marks between point and REGION-END."
  (if (and (< (point) region-end)
           (re-search-forward "[.,;:!?]" region-end t))
      (1+ (recursive-count-punctuation region-end))
    0))

(defun count-puncuation (beg end)
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (let ((count (recursive-count-punctuation end)))
      (cond ((zerop count)
             (message
              "The region does NOT have any punctuation marks."))
            ((= 1 count)
             (message
              "The region has 1 punctuation mark."))
            (t
             (message
              "The region has %d punctuation marks." count))))))