Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created December 28, 2014 08:40
Show Gist options
  • Save xuchunyang/7b346c1c76d63443dae8 to your computer and use it in GitHub Desktop.
Save xuchunyang/7b346c1c76d63443dae8 to your computer and use it in GitHub Desktop.
elisp-demo-03.el
;; List -- car/cdr/nthcdr/nth
(defvar my-list '(a b c) "My demo LIST")
my-list
(car my-list)
a
(cdr my-list)
(b c)
(cons 'a '(b c))
(a b c)
(length my-list)
3
(length ())
0
(nthcdr 0 my-list)
(a b c)
(nthcdr 1 my-list)
(b c)
(nthcdr 2 my-list)
(c)
(nthcdr 3 my-list)
nil
(defun my-nthcdr (n a-list)
"Implement nthcdr"
(while (> n 0)
(setq n (1- n))
(setq a-list (cdr a-list)))
(message "%S" a-list))
(my-nthcdr 0 my-list)
(my-nthcdr 1 my-list)
(my-nthcdr 2 my-list)
(my-nthcdr 3 my-list)
(nth 0 my-list)
(nth 1 my-list)
(defun my-nth (n list)
"Return nth element of LIST"
(car (nthcdr n list)))
(my-nth 2 my-list)
;; How to use loop?
(let ((num 0))
(while (< num 5)
(princ (format "%d\n" num))
(setq num (1+ num))))
(dotimes (i 5)
(princ (format "\n%d" i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment