-
Notifications
You must be signed in to change notification settings - Fork 1
/
std.scm
77 lines (63 loc) · 2.06 KB
/
std.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(define (not x) (if x #f #t))
(define (null? obj) (if (eqv? obj '()) #t #f))
(define (list . objs) objs)
(define (id obj) obj)
(define (flip f) (lambda (a b) (f b a)))
(define (curry f a) (lambda (b) (apply f (list a b))))
(define (compose f g) (lambda (x) (f (apply g x))))
(define zero? (curry = 0))
(define positive? (curry < 0))
(define negative? (curry > 0))
(define (odd? num) (= (mod num 2) 1))
(define even? (compose not odd?))
(define (foldr f end xs)
(if (null? xs)
end
(f (car xs) (foldr f end (cdr xs)))))
(define (foldl f accum lst)
(if (null? lst)
accum
(foldl f (f accum (car lst)) (cdr lst))))
(define fold foldl)
(define reduce foldr)
(define (unfold f init stop?)
(if (stop? init)
(cons init '())
(cons init (unfold f (f init) stop?))))
(define (sum . lst) (fold + 0 lst))
(define (product . lst) (fold * 1 lst))
(define (and . lst) (fold && #t lst))
(define (or . lst) (fold || #f lst))
(define (max first . rest)
(fold (lambda (old new)
(if (> old new) old new))
first
rest))
(define (min first . rest)
(fold (lambda (old new)
(if (< old new) old new))
first
rest))
(define (length lst)
(fold (lambda (x y) (+ x 1)) 0 lst))
(define (reverse lst) (fold (flip cons) '() lst))
(define (mem-helper pred op)
(lambda (acc next)
(if (and (not acc)
(pred (op next)))
next
acc)))
(define (memq obj lst) (fold (mem-helper (curry eq? obj) id) #f lst))
(define (memv obj lst) (fold (mem-helper (curry eqv? obj) id) #f lst))
(define (member obj lst) (fold (mem-helper (curry equal? obj) id) #f lst))
(define (assq obj alist) (fold (mem-helper (curry eq? obj) car) #f alist))
(define (assv obj alist) (fold (mem-helper (curry eqv? obj) car) #f alist))
(define (assoc obj alist) (fold (mem-helper (curry equal? obj) car) #f alist))
(define (map f lst) (foldr (lambda (x xs) (cons (f x) xs)) '() lst))
(define (filter keep? lst)
(foldr (lambda (x xs)
(if (keep? x)
(cons x xs)
xs))
'()
lst))