Skip to content

Instantly share code, notes, and snippets.

@futuro
Created February 9, 2014 22:20
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 futuro/8906897 to your computer and use it in GitHub Desktop.
Save futuro/8906897 to your computer and use it in GitHub Desktop.
;;; Example implementation of a character list
(define chr_list '(#\w #\h #\a #\t))
;;; Helper function to make reversing easy
(define (rev list)
(pushonto list '()))
;;; Function that does all of the work
(define (pushonto list1 list2)
;; If the first list is empty, return the second
(cond ((null? list1) list2)
;; Otherwise recurse
(else (pushonto (cdr list1)
;; Pushing the head of list1 onto list2, causing reversal
(cons (car list1) list2)))))
;;; The reverse of the reverse should be the original, so test that to make sure it is
(define (test-rev list)
(let ((reversed-list (rev list)))
(equal? (rev reversed-list) list)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment