-
Notifications
You must be signed in to change notification settings - Fork 17
/
lists.scm
108 lines (78 loc) · 2.25 KB
/
lists.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(define l (list 1 2 3 4 5 6 7 8 9 10))
(define (list? l)
(or (null? l) (and (pair? l) (list? (cdr l)))))
(define (list? l)
(if (null? l) #t
(and (pair? l) (list? (cdr l)))))
(define (length l)
(if (null? l) 0
(+ 1 (length (cdr l)))))
(define (length l)
(foldr (lambda (x r) (+ 1 r)) 0 l))
(define (list-ref l n) (car (list-tail l n)))
(define (list-tail l n)
(if (= n 0) l
(list-tail (cdr l) (- n 1))))
(define (repeated f n)
(lambda (x)
(if (= n 0) x
(f ((repeated f (- n 1)) x)))))
(define (list-tail l n)
((repeated cdr n) l))
(define (memberg equality)
(lambda (x l)
(cond ((null? l) #f)
((equality (car l) x) l)
(else (member x (cdr l))))))
(define member (memberg equal?))
(define memv (memberg eqv?))
(define memq (memberg eq?))
(define (from-to a b)
(if (> a b) '()
(cons a (from-to (+ a 1) b))))
(define (collect a b next)
(if (> a b) '()
(cons a (collect (next a) b next))))
(define (1+ x) (+ x 1))
(define (from-to a b) (collect a b 1+))
(define (foldr op nv l)
(if (null? l) nv
(op (car l) (foldr op nv (cdr l)))))
(define (append l1 l2)
(if (null? l1) l2
(cons (car l1) (append (cdr l1) l2))))
(define (append l1 l2)
(foldr cons l2 l1))
(define (reverse l)
(if (null? l) l
(append (reverse (cdr l)) (list (car l)))))
(define (reverse l)
(define (iter m r)
(if (null? m) r
(iter (cdr m) (cons (car m) r))))
(iter l '()))
(define (reverse l)
(foldr (lambda (x r) (append r (list x))) '() l))
(define (rcons l x) (cons x l))
(define (reverse l)
(foldl rcons '() l))
(define (map f l)
(if (null? l) l
(cons (f (car l)) (map f (cdr l)))))
(define (filter p? l)
(cond ((null? l) l)
((p? (car l)) (cons (car l) (filter p? (cdr l))))
(else (filter p? (cdr l)))))
(define (foldr op nv l)
(if (null? l) nv
(op (car l) (foldr op nv (cdr l)))))
(define (map f l)
(foldr (lambda (x r) (cons (f x) r)) '() l))
(define (filter p? l)
(foldr (lambda (x r) (if (p? x) (cons x r) r)) '() l))
(define (accumulate op nv a b term next)
(foldr op nv (map term (collect a b next))))
(define (id x) x)
(define (foldl op nv l)
(if (null? l) nv
(foldl op (op nv (car l)) (cdr l))))