Created
June 19, 2014 17:02
-
-
Save hrs/d187c8652951897da9ac to your computer and use it in GitHub Desktop.
Code used in my Emacs Lisp talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; An Introduction to Emacs Lisp | |
;;; Harry Schwartz, 2014 | |
;; What we'll be covering: | |
'(atoms | |
functions | |
lists | |
variables | |
defining-functions | |
conditionals | |
recursion | |
anonymous-functions | |
higher-order-functions | |
keybindings | |
built-in-documentation) | |
;; What we won't be covering: | |
most-of-the-emacs-standard-library ; :( | |
;; Emacs can be thought of as a big REPL. | |
;; In emacs-lisp-mode, "C-x C-e" evaluates a statement in the context | |
;; of the REPL. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Atoms | |
1 | |
3.0 | |
path-separator | |
"foo!" | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Functions | |
(+ 1 2 3) | |
(+ (* 2 3) | |
(/ 8 4)) | |
(message "hello, minibuffer!") | |
(insert " ; inserted text!") ; inserted text! | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Lists | |
(1 2 3) | |
(quote (1 2 3)) | |
'(1 2 3) | |
'(1 2 (3 4 5) (6 (7))) | |
(car '(1 2 3)) | |
(cdr '(1 2 3)) | |
'() | |
() | |
nil | |
(null nil) | |
(null '()) | |
(cdr '(42)) | |
(cons 1 '(2 3)) | |
(cons 1 (cons 2 (cons 3 '()))) | |
(append '(1 2) '(3 4)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Variables | |
(set 'some-list '(1 2 3)) | |
some-list | |
(setq my-list '(foo bar baz)) | |
my-list | |
(let ((a 1) | |
(b 5)) | |
(format "a is %d and b is %d" a b)) | |
a | |
(let* ((a 1) | |
(b (+ a 5))) | |
(format "a is %d and b is %d" a b)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Defining functions | |
(defun say-hello () | |
(message "hello!")) | |
(say-hello) | |
(defun square (x) | |
(* x x)) | |
(square 2) | |
(defun distance (x1 y1 x2 y2) | |
(sqrt (+ (square (- x2 x1)) | |
(square (- y2 y1))))) | |
(distance 3 0 0 4) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Conditionals | |
(when (= (+ 2 2) 4) | |
(message "passed sanity check!")) | |
(defun evens-or-odds (n) | |
(if (= 0 (% n 2)) | |
"even!" | |
"odd!")) | |
(evens-or-odds 4) | |
(evens-or-odds 3) | |
(defun pick-a-word (n) | |
(cond | |
((= n 1) "bulbous") | |
((= n 2) "bouffant") | |
((= n 3) "beluga") | |
(t "gazebo!"))) | |
(pick-a-word 2) | |
(pick-a-word nil) | |
(pick-a-word -72) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Recursion | |
(defun factorial (n) | |
(if (< n 1) | |
1 | |
(* n (factorial (- n 1))))) | |
(factorial 50) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Anonymous functions | |
(lambda (x) (* x x x)) | |
((lambda (x) (* x x x)) 5) | |
(fset 'cube (lambda (x) (* x x x))) | |
(setq cube "foo") | |
(cube 4) | |
cube | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Higher-order functions | |
(mapcar 'upcase '("foo" "bar" "baz")) | |
(mapcar 'upcase '()) | |
(oddp 3) | |
(oddp 4) | |
(remove-if-not 'oddp | |
'(0 1 2 3 4 5 6 7 8 9)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; An example | |
(defun qs (list) | |
"Non-destructively sort the | |
elements of `list' using quicksort." | |
(if (null list) | |
'() | |
(let* ((pivot (car list)) | |
(rest (cdr list)) | |
(lesser (remove-if-not | |
(lambda (x) (<= x pivot)) rest)) | |
(greater (remove-if-not | |
(lambda (x) (> x pivot)) rest))) | |
(append (qs lesser) (list pivot) (qs greater))))) | |
(qs '(3 5 7 8 4 2 5 7 9 0 0 8 4 6)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Keybindings | |
(global-set-key (kbd "M-#") 'sort-lines) | |
major-mode | |
(add-hook 'c-mode-common-hook | |
(lambda () | |
(local-set-key (kbd "<f5>") 'recompile))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Built-in documentation | |
(kbd "C-h k") | |
;; C-h k | |
(describe-key (kbd "C-h k")) | |
;; C-h a | |
(apropos-command "region") | |
;; C-h f | |
(describe-function 'qs) | |
;; C-h v | |
(describe-variable 'path-separator) | |
;; C-h m | |
(describe-mode) | |
;; C-h C-h | |
(help-for-help) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Further reference | |
;; Bastien's excellent "Learn #Emacs Lisp in 15 minutes" | |
(browse-url | |
"http://bzg.fr/learn-emacs-lisp-in-15-minutes.html") | |
;; The built-in "An Introduction to Programming in Emacs Lisp" | |
(info "(eintr) Top") | |
;; The built-in "Emacs Lisp Reference" | |
(info "(elisp) Top") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also a video of the talk and a fully written-up tutorial.