Last active
December 16, 2015 00:29
-
-
Save maksbotan/5348293 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 (empty_list selector) | |
(selector '() '() #t) | |
) | |
(define (prepend el lst) | |
(lambda (selector) | |
(selector el lst #f) | |
) | |
) | |
(define (head lst) | |
(lst (lambda (h t e) h)) | |
) | |
(define (tail lst) | |
(lst (lambda (h t e) t)) | |
) | |
(define (is_empty lst) | |
(lst (lambda (h t e) e)) | |
) | |
(define (not x) | |
(if x | |
#f | |
#t | |
) | |
) | |
(define (and a b) | |
(if a | |
(if b | |
#t | |
#f | |
) | |
#f | |
) | |
) | |
(define (or a b) | |
(if a | |
#t | |
(if b | |
#t | |
#f | |
) | |
) | |
) | |
(define (map fn l) | |
(if (is_empty l) | |
empty_list | |
(prepend (fn (head l)) (map fn (tail l))) | |
) | |
) | |
(define (filter fn l) | |
(if (is_empty l) | |
empty_list | |
(if (fn (head l)) | |
(prepend (head l) (filter fn (tail l))) | |
(filter fn (tail l)) | |
) | |
) | |
) | |
(define zero empty_list) | |
(define (inc n) | |
(prepend empty_list n) | |
) | |
(define (dec n) | |
(tail n) | |
) | |
(define one (inc zero)) | |
(define (add a b) | |
(if (is_zero b) | |
a | |
(add (inc a) (dec b)) | |
) | |
) | |
(define (sub a b) | |
(if (is_zero b) | |
a | |
(sub (dec a) (dec b)) | |
) | |
) | |
(define (mul a b) | |
(if (is_zero b) | |
zero | |
(add a (mul a (dec b))) | |
) | |
) | |
(define (pow a b) | |
(if (is_zero b) | |
one | |
(mul a (pow a (dec b))) | |
) | |
) | |
(define (is_equal n m) | |
(if (and (is_zero n) (is_zero m)) | |
#t | |
(if (or (is_zero n) (is zero m)) | |
#f | |
(is_equal (dec n) (dec m)) | |
) | |
) | |
) | |
(define (less_than a b) | |
(if (and (is_zero a) (is_zero b)) | |
#f | |
(if (is_zero a) | |
#t | |
(if (is_zero b) | |
#f | |
(less_than (dec a) (dec b)) | |
) | |
) | |
) | |
) | |
(define (greater_than a b) | |
(less_than b a) | |
) | |
(define (div a b) | |
(if (less_than a b) | |
zero | |
(inc (div (sub a b) b)) | |
) | |
) | |
(define (rem a b) | |
(if (less_than a b) | |
a | |
(rem (sub a b) b) | |
) | |
) | |
(define (nth l n) | |
(if (is_zero n) | |
(head l) | |
(nth (tail l) (dec n)) | |
) | |
) | |
(define (drop l n) | |
(if (is_zero n) | |
l | |
(drop (tail l) (dec n)) | |
) | |
) | |
(define (take l n) | |
(if (is_zero n) | |
empty_list | |
(prepend (head l) (take (tail l) (dec n))) | |
) | |
) | |
(define (slice l start end) | |
(take (drop l start) (sub end start)) | |
) | |
(define (length l) | |
(if (is_empty l) | |
zero | |
(inc (length (tail l))) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment