Created
June 1, 2009 05:00
-
-
Save twopoint718/121242 to your computer and use it in GitHub Desktop.
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
;; 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