-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.scm
203 lines (161 loc) · 5.24 KB
/
list.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;; insert and set
(define-syntax insert-set!
(syntax-rules ()
((_ expr var)
(set! var (insert expr var)))))
;; (define-syntax insert
;; (syntax-rules ()
;; ((_ el lst) (cons el lst))))
(define insert cons)
(define (only-one? expr)
(null? (rest expr)))
(define (pair-list? expr)
(and (list? expr) (only-one? (rest expr))))
(define (empty? lst)
(null? lst))
(define empty '())
;; insert an element in a list (at the end) if the element is not already included in the list (note: but element could be already many times in the list! and so in the result!)
;; (insertNoDups 'k '(a bc d e f a x y z d e t g))
;; '(a bc d e f a x y z d e t g k)
;; (insertNoDups 'k '(a bc d e f a x y k z d e t g))
;; '(a bc d e f a x y k z d e t g)
;; > (insertNoDups 'k '(a bc d e f a x y k z d e k t g))
;; '(a bc d e f a x y k z d e k t g)
;; (define (insertNoDups element lst)
;; (cond
;; ((empty? lst) (cons element lst))
;; ((equal? element (first lst)) lst)
;; (else (cons (first lst) (insertNoDups element (rest lst))))))
;; (define (insertNoDups element lst)
;; (if (member element lst)
;; lst
;; (reverse (cons element (reverse lst))))) ;; keep order
;; insert an element in a list (at the begin) if the element is not already included in the list (note: but element could be already many times in the list! and so in the result!))
;; (insertNoDups 'k '(a bc d e f a x y z d e t g))
;; '(k a bc d e f a x y z d e t g)
;; (insertNoDups 'k '(a bc d e f a x y k z d e t g))
;; '(a bc d e f a x y k z d e t g)
;; > (insertNoDups 'k '(a bc d e f a x y k z d e k t g))
;; '(a bc d e f a x y k z d e k t g)
(define (insertNoDups element lst)
(if (member element lst)
lst
(cons element lst)))
;; (remove-duplicates '(a bc d e f a x y z d e t g)) -> '(g t e d z y x a f bc)
(define (remove-duplicates lst)
(cond
((empty? lst) empty)
(else (insertNoDups (first lst) (remove-duplicates (rest lst)))))) ;; insert in a list that has NO MORE duplicates !
(define (singleton-list? lst)
(and (list? lst) (null? (rest lst))))
;;> (create-list '() 5) -> '(() () () () ())
;;> (create-list 'x 5) -> '(x x x x x)
(define (create-list elem lgt)
(if (= 0 lgt)
'()
(cons elem (create-list elem (- lgt 1)))))
;; remove duplicates but keep the list sorted
;; (remove-duplicates-sorted '(A A B C D D E F G)) -> '(A B C D E F G)
;; DEPRECATED because same as uniq
;; (define (remove-duplicates-sorted sorted-lst)
;; (reverse (remove-duplicates sorted-lst)))
;; like 'uniq' UNIX command but on List (suppose list is already sorted or at least identical elements are clustered)
;; (uniq '(A A B C D D E F G))
;;'(A B C D E F G)
(define (uniq L)
(cond
((null? L) '())
(else
(cons (car L)
(uniq (remove-firsts-elements (car L)
(cdr L)))))))
;; remove all the c in '(c c c c c c ... L)
(define (remove-firsts-elements c L)
(cond
((null? L) '())
;; ((null? (cdr L)) (if (equal? c (car L))
;; '()
;; L))
((not (equal? c (car L))) L)
(else
(remove-firsts-elements c (cdr L)))))
;; remove last element of a list
;;
;; > (remove-last '(((1 0 0 0)) ((0 1 0 1) (1 0 1 0) (1 1 0 0)) ((0 1 1 1) (1 1 0 1) (1 1 1 0)) ((1 1 1 1))))
;; '(((1 0 0 0)) ((0 1 0 1) (1 0 1 0) (1 1 0 0)) ((0 1 1 1) (1 1 0 1) (1 1 1 0)))
;; > (remove-last '(a b c))
;; '(a b)
(define (remove-last lst)
(reverse (rest (reverse lst))))
;; > (replace '(1 (1 2 3 4 (5 6 3) 3 4)) 3 7)
;; '(1 (1 2 7 4 (5 6 7) 7 4))
;; > (replace '() 3 7)
;; '()
;; > (replace '(1 (1 2 3 4) 3 4) 3 7)
;; '(1 (1 2 7 4) 7 4)
;; (define (replace L new old)
;; (cond ;;((null? L) L)
;; ((list? L)
;; (map
;; (lambda (lst) (replace lst new old))
;; L))
;; (else
;; (if (equal? L old)
;; new
;; L))))
;; > (replace '(1 (1 2 3 4) 3 4) 3 7)
;; '(1 (1 2 7 4) 7 4)
;; > (replace '() 7 3)
;; '()
;; > (replace '(1 (1 2 3 4) 3 4) 3 7)
;; '(1 (1 2 7 4) 7 4)
;; > (replace '(1 (1 2 3 4 (5 6 3) 3 4)) 3 7)
;; '(1 (1 2 7 4 (5 6 7) 7 4))
;;
;; (replace 4 4 5) -> 5
;; warning : element to replace must not be () !!!
(define (replace L old new)
(if (list? L)
(map
(lambda (lst) (replace lst old new))
L)
(if (equal? L old)
new
L)))
(define (debut n L)
(if (or (null? L) (= n 0))
'()
(cons (first L) (debut (- n 1) (rest L)))))
(define (debut-iter n L)
(define (iter acc ncur Lcur)
(if (or (null? Lcur) (= ncur 0))
acc
(iter (cons (first Lcur) acc) (- ncur 1) (rest Lcur))))
(reverse (iter '() n L)))
;; scheme@(guile-user)> (not-list? 4)
;; $1 = #t
(define (not-list? L)
(not (list? L)))
;; (before-element 5 '(20 10 5 14 7)) -> '(20 10)
;; (before-element 20 '(20 10 5 14 7)) -> '()
(define (before-element x L)
(letrec ((be-rec (lambda (L)
(if (equal? (first L) x)
'()
(cons (first L) (be-rec (rest L)))))))
(be-rec L)))
;; (start-with-element 5 '(20 10 5 14 7)) -> '(5 14 7)
(define (start-with-element x L)
(letrec ((aft-rec (lambda (L)
(if (equal? (first L) x)
L
(aft-rec (rest L))))))
(aft-rec L)))
;; scheme@(guile-user)> (pair-list-elements '(a b c d e f))
;; $2 = ((a b) (c d) (e f))
(define (pair-list-elements L)
(if (null? L)
L
(cons (list (first L)
(first (rest L)))
(pair-list-elements (rest (rest L))))))