Last active
December 29, 2020 23:18
-
-
Save qguv/570ce1b23b78cb786231855f0e08676f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
; this can be evaluated at https://inst.eecs.berkeley.edu/~cs61a/fa14/assets/interpreter/scheme.html | |
(define | |
(square x) | |
(* x x) | |
) | |
(define | |
(range start length) | |
(if | |
(= start length) | |
'() | |
(cons start (range (+ start 1) length)) | |
) | |
) | |
(define | |
(map f xs) | |
(if | |
(null? xs) | |
xs | |
(cons | |
(f (car xs)) | |
(map f (cdr xs)) | |
) | |
) | |
) | |
(define | |
(sum xs) | |
(if | |
(null? xs) | |
0 | |
(+ (car xs) (sum (cdr xs))) | |
) | |
) | |
(define | |
(fiboid a b reps) | |
(if | |
(< reps 1) | |
'() | |
(cons b (fiboid b (+ a b) (- reps 1))) | |
) | |
) | |
(define (fib n) (fiboid 0 1 n)) | |
(define | |
(reduce fn xs init) | |
(if | |
(null? xs) | |
init | |
(reduce fn (cdr xs) (fn init (car xs))) | |
) | |
) | |
(define | |
(all xs) | |
(cond | |
((null? xs) #t) | |
((not (car xs)) #f) | |
(else (all (cdr xs))) | |
) | |
) | |
(define | |
(any xs) | |
(cond | |
((null? xs) #f) | |
((car xs) #t) | |
(else (any (cdr xs))) | |
) | |
) | |
(define | |
(filter fn xs) | |
(cond | |
((null? xs) '()) | |
((fn (car xs)) (cons (car xs) (filter fn (cdr xs)))) | |
(else (filter fn (cdr xs))) | |
) | |
) | |
(define | |
(reduce fn xs init) | |
(if | |
(null? xs) | |
init | |
(reduce fn (cdr xs) (fn init (car xs))) | |
) | |
) | |
(define | |
(squeeze fn) | |
(lambda (xs) (apply fn xs)) | |
) | |
(define | |
(spread fn) | |
(lambda (. args) (fn args)) | |
) | |
(define | |
(rev- old new) | |
(if | |
(null? old) | |
new | |
(rev- (cdr old) (cons (car old) new)) | |
) | |
) | |
(define (rev xs) (rev- xs '())) | |
(define | |
(compose f g) | |
(lambda (. args) (f (apply g args))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment