Created
September 6, 2018 00:45
-
-
Save iErik/5c15c34ab86393d3fbe3f4584a517a0d 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
(define atom? | |
(lambda (x) | |
(and (not (pair? x)) (not (null? x))))) | |
(define lat? | |
(lambda (l) | |
(cond | |
((null? l) #t) | |
((atom? (car l)) (lat? (cdr l))) | |
(else #f)))) | |
(define member? | |
(lambda (a lat) | |
(cond | |
((null? lat) #f) | |
(else (or (eq? (car lat) a) | |
(member? a (cdr lat))))))) | |
; rember without cons | |
(define no-rember | |
(lambda (a lat) | |
(cond | |
((null? lat) (quote ())) | |
(else (cond | |
((eq? (car lat) a) (cdr lat)) | |
(else (rember a | |
(cdr lat)))))))) | |
; rember with cons | |
(define rember | |
(lambda (a lat) | |
(cond | |
((null? lat) (quote ())) | |
(else (cond | |
((eq? (car lat) a) (cdr lat)) | |
(else (cons (car lat) | |
(rember a | |
(cdr lat)))))))) | |
; The function rember checked each atom of the lat, | |
; one at a time, to see if it was the same as the | |
; atom 'and'. If the car was not the same as the | |
; atom, we saved it to be consed to the final | |
; value later. When rember found the atom 'and', | |
; it dropped it, and consed the previous atoms | |
; back onto the rest of the lat. | |
; rember simplified | |
(define rember | |
(lambda (a lat) | |
(cond | |
((null? lat) (quote ())) | |
((eq? (car lat) a) (cdr lat)) | |
(else (cons (car lat) | |
(rember a (cdr lat))))))) | |
(define firsts | |
(lambda (l) | |
(cond | |
((null? l) (quote ())) | |
(else (cons (car (car l)) (firsts (cdr l))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment