Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created June 1, 2009 05:00
Show Gist options
  • Save twopoint718/121242 to your computer and use it in GitHub Desktop.
Save twopoint718/121242 to your computer and use it in GitHub Desktop.
;; slow because appending to a list means that
;; we have to traverse it over and over again
(defun my-rev-1 (lst)
(if (null lst)
'()
(append (my-rev-1 (cdr lst)) (list (car lst)))))
;; better because we use an empty list that we can
;; cons onto the front of (which is fast)
;; "labels" is a way to make a private recursive
;; function
(defun my-rev-2 (lst)
(labels ((rec (list new-list)
(if (null list)
new-list
(rec (cdr list) (cons (car list) new-list)))))
(rec lst '())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment