Skip to content

Instantly share code, notes, and snippets.

@fselcukcan
Last active May 31, 2019 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fselcukcan/1b013def06ab6df66f3532ed5131bb52 to your computer and use it in GitHub Desktop.
Save fselcukcan/1b013def06ab6df66f3532ed5131bb52 to your computer and use it in GitHub Desktop.
reverse
(defun reverse (l acc)
(cond
((null l) acc)
(t (reverse (cdr l) (cons (car l) acc)))))
; (reverse '(7 5 1 3)) => (3 1 5 7)
(define reverse
(lambda (l acc)
(cond
((null? l) acc)
(else (reverse (cdr l) (cons (car l) acc))))))
; (reverse '(7 5 1 3) '()) => (3 1 5 7)
;; can be written with a helper function
(define reverse
(lambda (l)
(reverse-aux l (quote ()))))
(define reverse-aux
(lambda (l acc)
(cond
((null? l) acc)
(else (reverse (cdr l) (cons (car l) acc))))))
; (reverse '(7 5 1 3)) => (3 1 5 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment