Skip to content

Instantly share code, notes, and snippets.

@jsgoller1
Created April 4, 2020 23:15
Show Gist options
  • Save jsgoller1/407c0f326f69d6f37a9aa9dbe380c69a to your computer and use it in GitHub Desktop.
Save jsgoller1/407c0f326f69d6f37a9aa9dbe380c69a to your computer and use it in GitHub Desktop.
list-eq?
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
(define generic-eq?
(lambda (item1 item2)
(cond ((and (number? item1) (number? item2)) (= item1 item2))
((and (atom? item1) (atom? item2)) (eq? item1 item2))
((and (list? item1) (list? item2)) (list-eq? item1 item2))
(else #f)
)))
(generic-eq? 1 1) ; #t
(generic-eq? 1 2) ; #f
(generic-eq? 'joshua 'joshua) ; #t
(generic-eq? 'elliott 'joshua) ; #f
(define list-eq?
(lambda (list1 list2)
(if (and
(null? list1)
(null? list2))
#t
(and
(generic-eq? (car list1) (car list2))
(list-eq? (cdr list1)(cdr list2))))))
(list-eq? '(1 2 3) '(1 2 3)) ; #t
(list-eq? '(1 elliott 3) '(1 elliott 3)) ; #t
(list-eq? '(1 (elliott) 3) '(1 (elliott) 3)) ; #t
(list-eq? '(1 (elliott) 3) '(1 (eliot) 3)) ; #f
(list-eq? '(1 () 3) '(1 () 3)) ; #t
(list-eq? '(1 ((())) 3) '(1 ((())) 3)) ; #t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment